如何在PyQt中绑定主窗口和对话框

时间:2019-01-07 14:48:47

标签: python pyqt qt-designer

我在Qt Designer中创建了一个主窗口和一个对话框。在主窗口中有一个按钮,当我按下该按钮时我想打开对话框。

我将这两个ui文件转换为python文件,并创建了一个新的python文件,该文件调用主窗口。

这是从ui转换而来的mainwindow.py

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'deneme.ui'
#
# Created by: PyQt4 UI code generator 4.12.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from dialog import Ui_Dialog
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(685, 353)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(240, 66, 331, 81))
        self.label.setObjectName(_fromUtf8("label"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(280, 200, 97, 27))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 685, 25))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.dialogbox)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:26pt;\">LNL Technology</span></p></body></html>", None))
        self.pushButton.setText(_translate("MainWindow", "Tamam", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

这是dialog.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'dialogbox.ui'
#
# Created by: PyQt4 UI code generator 4.12.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 211)
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(70, 70, 231, 71))
        self.label.setObjectName(_fromUtf8("label"))

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

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "<html><head/><body><p><span style=\" font-size:28pt;\">Guzel miyiz?</span></p></body></html>", None))

最后,这是我为调用mainwindow而编写的main.py

from PyQt4.QtGui import *
from PyQt4.QtCore import *

from mainwindow import Ui_MainWindow
from dialog import Ui_Dialog

class AnaPencere(QMainWindow):
    def __init__(self,ebeveyn=None):
        QWidget.__init__(self,ebeveyn)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    def dialogbox(self):
        #QWidget.__init__(self)
        ui = Ui_Dialog()
        ui.setupUi(self)

uyg=QApplication([])
pencere=AnaPencere()
pencere.show()
uyg.exec_()

当我单击按钮时,窗口大小已更改,但文本仍然相同。我的目的是为对话框打开一个新窗口。但是我不知道我能做什么。如何将对话框声明为新窗口?

0 个答案:

没有答案