Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

python - Embedding a terminal in PyQt5

So I've been trying to create my own terminal but that has been proven very glitchy and not professional looking.

Then I stumbled across this code which is for PyQt4:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class embterminal(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        self.process = QProcess(self)
        self.terminal = QWidget(self)
        layout = QVBoxLayout(self)
        layout.addWidget(self.terminal)
        #self.process.start(
                #'xterm',['-into', str(self.terminal.winId())])
        # Works also with urxvt:
        self.process.start(
                'urxvt',['-embed', str(self.terminal.winId())])


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = embterminal()
    main.show()
    sys.exit(app.exec_())

Since my application is written in PyQt5, I naturally tried porting that code to PyQt5. I changed from PyQt4.QtCore import * from PyQt4.QtGui import * to from PyQt5.QtCore import * from PyQt5.QtGui import * and added from PyQt5.QtWidgets import *

Then when I ran my code I realized the terminal didn't pop up.

I wonder why does this happen and is there a workaround ?

I also wonder if I can use both PyQt5 and PyQt4 in the same project/file, even.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In PyQt. QWidget.winId() returns a sip.voidptr object, but if you convert it to an integer, it should work. Here's a working example:

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.process = QProcess(self)
        self.terminal = QWidget(self)
        layout = QVBoxLayout(self)
        layout.addWidget(self.terminal)
        wid = str(int(self.terminal.winId()))
        self.process.start('urxvt', ['-embed', wid])

    def closeEvent(self, event):
        self.process.terminate()
        self.process.waitForFinished(1000)

if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = Window()
    window.setGeometry(100, 100, 800, 600)
    window.show()
    sys.exit(app.exec_())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...