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.
In PyQt5. winId() returns a <sip.voidptr object at 0x7ff710025aa8>
if you convert it to an integer then it will work.
Here's the working code:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets 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)
# Works also with urxvt:
self.process.start(
'urxvt',['-embed', str(int(self.winId()))])
print(self.winId())
self.setGeometry(1, 1, 800, 600)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = embterminal()
main.show()
sys.exit(app.exec_())
来源:https://stackoverflow.com/questions/51975678/embedding-a-terminal-in-pyqt5