2012-11-22 18 views
6

Varolan bir python3 ve Qt4 kullanarak yeni bir pencere aramaya çalışıyorum.Varolan bir pencereden yeni PyQt4 pencereleri nasıl oluşturulur?

Qt Designer (ana uygulama ve bir diğeri) kullanarak iki pencere oluşturdum ve Qt Designer tarafından oluşturulan .ui dosyalarını .py komut dosyalarına dönüştürdüm - ancak yeni pencereler oluşturamıyorum ana uygulamadan.

bunu yaparken çalıştı:

############### MAIN APPLICATION SCRIPT ################ 

from PyQt4 import QtCore, QtGui 
import v2 

try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    _fromUtf8 = lambda s: s 

class Ui_Form(object): 
    def setupUi(self, Form): 
     Form.setObjectName(_fromUtf8("Form")) 
     Form.resize(194, 101) 
     self.button1 = QtGui.QPushButton(Form) 
     self.button1.setGeometry(QtCore.QRect(50, 30, 99, 23)) 
     self.button1.setObjectName(_fromUtf8("button1")) 

     self.retranslateUi(Form) 
     QtCore.QMetaObject.connectSlotsByName(Form) 

    def retranslateUi(self, Form): 
     Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) 
     self.button1.setText(QtGui.QApplication.translate("Form", "Ventana", None, QtGui.QApplication.UnicodeUTF8)) 

     self.button1.connect(self.button1, QtCore.SIGNAL(_fromUtf8("clicked()")), self.mbutton1) 

    def mbutton1(self): 
     v2.main() 



if __name__ == "__main__": 
    import sys 
    app = QtGui.QApplication(sys.argv) 
    Form = QtGui.QWidget() 
    ui = Ui_Form() 
    ui.setupUi(Form) 
    Form.show() 
    sys.exit(app.exec_()) 
################## SECOND WINDOW ####################### 

from PyQt4 import QtCore, QtGui 

try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    _fromUtf8 = lambda s: s 

class Ui_Form(object): 
    def setupUi(self, Form): 
     Form.setObjectName(_fromUtf8("Form")) 
     Form.resize(400, 300) 
     self.label = QtGui.QLabel(Form) 
     self.label.setGeometry(QtCore.QRect(160, 40, 57, 14)) 
     self.label.setObjectName(_fromUtf8("label")) 

     self.retranslateUi(Form) 
     QtCore.QMetaObject.connectSlotsByName(Form) 

    def retranslateUi(self, Form): 
     Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) 
     self.label.setText(QtGui.QApplication.translate("Form", "LABEL 2", None, QtGui.QApplication.UnicodeUTF8)) 

def main(): 
    import sys 
    app = QtGui.QApplication(sys.argv) 
    Form = QtGui.QWidget() 
    ui = Ui_Form() 
    ui.setupUi(Form) 
    Form.show() 
    sys.exit(app.exec_()) 

Ama bu hata mesajı alıyorum:

QCoreApplication::exec: The event loop is already running 
QPixmap: Must construct a QApplication before a QPaintDevice 

cevap

12

pyuic, -x, --execute seçeneği ile çalıştırılabilir komut dosyaları oluşturabilir, ancak çoğunlukla sınama amaçlıdır.

pyuic temel amacı uygulamanıza ithalat içerdiği GUI sınıfları için izin Qt Desgner ui dosyalarından statik piton modülleri oluşturmaktır.

Diyelim ki Qt Designer kullanarak iki ui dosyaları oluşturulur ve onlara v1.ui ve v2.ui adlı varsayalım.

Sonra böyle iki piton modülleri yaratacak:

pyuic4 -o v1.py v1.ui 
pyuic4 -o v2.py v2.ui 

Ardından, modüllerden GUI sınıfları ithal ediyor ve gerektiğinde bunların örneklerini oluşturur ayrı main.py senaryo yazardı.Ben ad çatışmaları önlemek için hafifçe GUI sınıfların isimleri değişti

from PyQt4 import QtGui 
from v1 import Ui_Form1 
from v2 import Ui_Form2 

class Form1(QtGui.QWidget, Ui_Form1): 
    def __init__(self, parent=None): 
     QtGui.QWidget.__init__(self, parent) 
     self.setupUi(self) 
     self.button1.clicked.connect(self.handleButton) 
     self.window2 = None 

    def handleButton(self): 
     if self.window2 is None: 
      self.window2 = Form2(self) 
     self.window2.show() 

class Form2(QtGui.QWidget, Ui_Form2): 
    def __init__(self, parent=None): 
     QtGui.QWidget.__init__(self, parent) 
     self.setupUi(self) 

if __name__ == '__main__': 

    import sys 
    app = QtGui.QApplication(sys.argv) 
    window = Form1() 
    window.show() 
    sys.exit(app.exec_()) 

Not:

Yani senin main.py şuna benzer olabilir. GUI sınıflarını daha iyi adlar vermek için, Qt Desgner'daki en üst düzey sınıfın objectName özelliğini ayarlayın. Ve değişikliklerinizi yaptıktan sonra pyuic'u yeniden çalıştırmayı unutmayın!

5

Sadece bir tane QApplication oluşturabilirsiniz. Oluşturduktan sonra kaç pencere istediğinizi oluşturabilirsiniz. Örneğin

:

from PyQt4 import QtGui, QtCore 

class MyWindow(QtGui.QDialog): # any super class is okay 
    def __init__(self, parent=None): 
     super(MyWindow, self).__init__(parent) 
     self.button = QtGui.QPushButton('Press') 
     layout = QtGui.QHBoxLayout() 
     layout.addWidget(self.button) 
     self.setLayout(layout) 
     self.button.clicked.connect(self.create_child) 
    def create_child(self): 
     # here put the code that creates the new window and shows it. 
     child = MyWindow(self) 
     child.show() 


if __name__ == '__main__': 
    # QApplication created only here. 
    app = QtGui.QApplication([]) 
    window = MyWindow() 
    window.show() 
    app.exec_() 

Her zaman yeni bir pencere oluşturur butonuna tıklayın.

Yukarıdaki örneği Tasarımcısı ile oluşturduğunuz pencereleri kullanmak için uyarlayabilirsiniz. Yan not

:

hiç pyuic sonucunu düzenlemek asla. Bu dosyalar değiştirilmemelidir. Bu, Ui_Form'a mbutton1 yöntemini eklemeyin. Eğer pyuic tarafından oluşturulan dosyayı mywindow_ui.py varsa

, ardından dosyayı mywindow.py oluşturup böyle bir şey koymak: ana dosyadan Şimdi

from PyQt4 import QtCore, QtGui 
from mywindow_ui import Ui_MyWindow 

class MyWindow(QtGui.QWidget, Ui_MyWindow): #or whatever Q*class it is 
    def __init__(self, parent=None): 
     super(MyWindow, self).__init__(parent) 
     self.setupUi(self) 
    def create_child(self): #here should go your mbutton1 
     # stuff 
#etc. 

main.py yapmanız:

from PyQt4 import QtGui 

from mywindow import MyWindow 


# ... 

if __name__ == '__main__': 
    app = QtGui.QApplication([]) 
    window = MyWindow() 
    window.show() 
    app.exec_() 
İlgili konular