2014-02-05 30 views
0

我已經創建了文本編輯程序,並且我想添加其他功能,例如按鈕。我需要將它嵌入到小部件中(像文本編輯器是gui的面板,按鈕也像面板)對於java引用感到抱歉,不知道它們在pyqt4中被稱爲什麼,但我不知道如何使用PQT4和Python 3.x來做到這一點。 我要實現以下目標:! [請在此輸入圖像說明] [1]將文本編輯器嵌入到小部件中

這裏是我的文本編輯器

#! /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() 

任何建議碼?

+0

問題太寬泛,請註明。 – Schollii

回答

0

由於您沒有指定「另一個GUI」是什麼,我只是假設它是另一個PyQt4程序。在這種情況下,你所要做的就是使QMainWindow的行爲像一個普通的小部件。

首先,改變Notepad.__init__以便它可以有一個父:

class Notepad(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
     super(Notepad, self).__init__(parent) 
     ... 

然後調整window flags所以它不會像一個頂級窗口:

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) 
     ... 
+0

我編輯了我的問題 –

+0

@ Pro-grammer。你的問題仍然不清楚。請解釋爲什麼我的答案不是解決方案。 – ekhumoro

+0

我解決了這個問題,我需要做一個繼承一個小部件的類:)謝謝你指出我正確的方向:-) –

相關問題