所以我一直在尝试创建自己的终端,但事实证明这非常有问题,而且看起来不专业.
So I've been trying to create my own terminal but that has been proven very glitchy and not professional looking.
然后我偶然发现了这段用于 PyQt4 的代码:
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_())
由于我的应用程序是用 PyQt5 编写的,我自然而然地尝试将该代码移植到 PyQt5.我从 PyQt4.QtCore import * 更改了 从 PyQt4.QtGui 导入 *
到 从 PyQt5.QtCore 导入 *from PyQt5.QtGui import *
并添加 from PyQt5.QtWidgets import *
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 ?
我也想知道我是否可以在同一个项目/文件中同时使用 PyQt5 和 PyQt4.
I also wonder if I can use both PyQt5 and PyQt4 in the same project/file, even.
在 PyQt.QWidget.winId()
返回一个 sip.voidptr
对象,但如果将其转换为整数,它应该可以工作.这是一个工作示例:
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_())
这篇关于在 PyQt5 中嵌入终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!