Python 将文本编辑器嵌入小部件
我已经创建了一个文本编辑程序,我想添加其他功能,如按钮。我需要将它嵌入到小部件中(就像文本编辑器是gui上的一个面板,按钮也是一个面板)抱歉,对于java参考,我不知道它们在pyqt4中被称为什么,但我不知道如何使用PQT4和Python3.x来实现这一点。 我希望实现以下目标: ![在此处输入图像描述][1] 这是我的文本编辑器代码Python 将文本编辑器嵌入小部件,python,pyqt,pyqt4,Python,Pyqt,Pyqt4,我已经创建了一个文本编辑程序,我想添加其他功能,如按钮。我需要将它嵌入到小部件中(就像文本编辑器是gui上的一个面板,按钮也是一个面板)抱歉,对于java参考,我不知道它们在pyqt4中被称为什么,但我不知道如何使用PQT4和Python3.x来实现这一点。 我希望实现以下目标: ![在此处输入图像描述][1] 这是我的文本编辑器代码 #! /usr/bin/python import sys import os from PyQt4 import QtGui class Notepad(Q
#! /usr/bin/python
import sys
import os
from PyQt4 import QtGui
class Notepad(QtGui.QMainWindow):
def __init__(self):
super(Notepad, self).__init__()
self.initUI()
def initUI(self):
newAction = QtGui.QAction('New', self)
newAction.setShortcut('Ctrl+N')
newAction.setStatusTip('Create new file')
newAction.triggered.connect(self.newFile)
saveAction = QtGui.QAction('Save', self)
saveAction.setShortcut('Ctrl+S')
saveAction.setStatusTip('Save current file')
saveAction.triggered.connect(self.saveFile)
openAction = QtGui.QAction('Open', self)
openAction.setShortcut('Ctrl+O')
openAction.setStatusTip('Open a file')
openAction.triggered.connect(self.openFile)
closeAction = QtGui.QAction('Close', self)
closeAction.setShortcut('Ctrl+Q')
closeAction.setStatusTip('Close Notepad')
closeAction.triggered.connect(self.close)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(newAction)
fileMenu.addAction(saveAction)
fileMenu.addAction(openAction)
fileMenu.addAction(closeAction)
self.text = QtGui.QTextEdit(self)
self.setCentralWidget(self.text)
self.setGeometry(300,300,500,500)
self.setWindowTitle('Pygame Simplified text editor')
self.show()
def newFile(self):
self.text.clear()
def saveFile(self):
filename = QtGui.QFileDialog.getSaveFileName(self, 'Save File', os.getenv('HOME'))
f = open(filename, 'w')
filedata = self.text.toPlainText()
f.write(filedata)
f.close()
def openFile(self):
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', os.getenv('HOME'))
f = open(filename, 'r')
filedata = f.read()
self.text.setText(filedata)
f.close()
def main():
app = QtGui.QApplication(sys.argv)
notepad = Notepad()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
有什么建议吗?既然您没有指定“另一个GUI”是什么,我就假设它是另一个PyQt4程序。在这种情况下,您所要做的就是使QMainWindow的行为像一个普通的小部件 首先,更改记事本。\uuuu init\uuuu,以便它可以有一个父项:
class Notepad(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Notepad, self).__init__(parent)
...
然后调整,使其行为不像顶级窗口:
class AnotherGUI(QtGui.QMainWindow):
def __init__(self):
super(AnotherGUI, self).__init__()
self.notepad = Notepad(self)
self.notepad.setWindowFlags(
self.notepad.windowFlags() & ~QtCore.Qt.Window)
self.button = QtGui.QPushButton('Submit', self)
...
问题太宽泛,请指定。@programmar。你的问题还完全不清楚。请解释为什么我的答案不是一个解决方案。我解决了这个问题,我需要创建一个继承小部件的类:)谢谢你为我指明了正确的方向:-)