我的MainWindow中有36个pushButton,每个pushButton必须打开一个不同的QDialog窗口,但基于相同的模型。
当按钮很少时,我没有任何问题,但在我的情况下它太重了。所以我希望它是自动的。
解释我的问题非常困难所以我做了一个简单的例子,说明了我希望程序做什么。你可以在那里得到它:https://drive.google.com/open?id=0B_K-NA5YDW-nc01jZ29wLTd1WE0(运行mainCode.py)。
我的想法是取代它:
self.connect(self.ui.pushButton1,SIGNAL('clicked()'),self.interfaceDialog1)
self.connect(self.ui.pushButton2,SIGNAL('clicked()'),self.interfaceDialog2)
self.connect(self.ui.pushButton3,SIGNAL('clicked()'),self.interfaceDialog3)
self.connect(self.ui.pushButton4,SIGNAL('clicked()'),self.interfaceDialog4)
self.connect(self.ui.pushButton5,SIGNAL('clicked()'),self.interfaceDialog5)
self.connect(self.ui.pushButton6,SIGNAL('clicked()'),self.interfaceDialog6)
#etc...
通过以下方式:
for K in range(1,37):
self.connect(self.ui.pushButton1K,SIGNAL('clicked()'),self.interfaceDialogK)
并取而代之:
def interfaceDialog1(self):
self.dialog1 = Interface_Dialog()
self.dialog1.setWindowTitle('Dialog 1')
self.dialog1.ui.label.setText('Fenetre 1')
self.dialog1.show()
def interfaceDialog2(self):
self.dialog2 = Interface_Dialog()
self.dialog2.setWindowTitle('Dialog 2')
self.dialog2.ui.label.setText('Fenetre 2')
self.dialog2.show()
def interfaceDialog3(self):
self.dialog3 = Interface_Dialog()
self.dialog3.setWindowTitle('Dialog 3')
self.dialog3.ui.label.setText('Fenetre 3')
self.dialog3.show()
def interfaceDialog4(self):
self.dialog4 = Interface_Dialog()
self.dialog4.setWindowTitle('Dialog 4')
self.dialog4.ui.label.setText('Fenetre 4')
self.dialog4.show()
def interfaceDialog5(self):
self.dialog5 = Interface_Dialog()
self.dialog5.setWindowTitle('Dialog 5')
self.dialog5.ui.label.setText('Fenetre 5')
self.dialog5.show()
def interfaceDialog6(self):
self.dialog6 = Interface_Dialog()
self.dialog6.setWindowTitle('Dialog 6')
self.dialog6.ui.label.setText('Fenetre 6')
self.dialog6.show()
#etc....[/CODE]
by:
for k in range(1,37):
def interfaceDialogk(self):
self.dialogk = Interface_Dialog()
self.dialogk.setWindowTitle('Dialog {}'.format(k))
self.dialogk.ui.label.setText('Fenetre {}'.format(k))
self.dialogk.show()
但我不知道该怎么做......有什么想法吗?
感谢您的帮助
亚历山大
编辑: 这是我的mainCode
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from interface_main import Ui_MainWindow
from interface_dialog import Ui_Dialog
###--------------------------------------------------------------------------------------------------------------------------------------------------------------###
class Interface_Dialog(QDialog):
def __init__(self):
QDialog.__init__(self)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
###--------------------------------------------------------------------------------------------------------------------------------------------------------------###
class Interface_Main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.connect(self.ui.pushButton1,SIGNAL('clicked()'),self.interfaceDialog1)
self.connect(self.ui.pushButton2,SIGNAL('clicked()'),self.interfaceDialog2)
self.connect(self.ui.pushButton3,SIGNAL('clicked()'),self.interfaceDialog3)
self.connect(self.ui.pushButton4,SIGNAL('clicked()'),self.interfaceDialog4)
self.connect(self.ui.pushButton5,SIGNAL('clicked()'),self.interfaceDialog5)
self.connect(self.ui.pushButton6,SIGNAL('clicked()'),self.interfaceDialog6)
#etc...
def interfaceDialog1(self):
self.dialog1 = Interface_Dialog()
self.dialog1.setWindowTitle('Dialog 1')
self.dialog1.ui.label.setText('Fenetre 1')
self.dialog1.show()
def interfaceDialog2(self):
self.dialog2 = Interface_Dialog()
self.dialog2.setWindowTitle('Dialog 2')
self.dialog2.ui.label.setText('Fenetre 2')
self.dialog2.show()
def interfaceDialog3(self):
self.dialog3 = Interface_Dialog()
self.dialog3.setWindowTitle('Dialog 3')
self.dialog3.ui.label.setText('Fenetre 3')
self.dialog3.show()
def interfaceDialog4(self):
self.dialog4 = Interface_Dialog()
self.dialog4.setWindowTitle('Dialog 4')
self.dialog4.ui.label.setText('Fenetre 4')
self.dialog4.show()
def interfaceDialog5(self):
self.dialog5 = Interface_Dialog()
self.dialog5.setWindowTitle('Dialog 5')
self.dialog5.ui.label.setText('Fenetre 5')
self.dialog5.show()
def interfaceDialog6(self):
self.dialog6 = Interface_Dialog()
self.dialog6.setWindowTitle('Dialog 6')
self.dialog6.ui.label.setText('Fenetre 6')
self.dialog6.show()
#etc...
###--------------------------------------------------------------------------------------------------------------------------------------------------------------###
#Test de l'application
if __name__=="__main__":
appUI = QApplication(sys.argv)
interface = Interface_Main()
interface.show()
sys.exit(appUI.exec_())
和2个文件由QtDesigner创建并由我的mainCode使用。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'interface_main.ui'
#
# Created: Sun Jun 19 12:34:47 2016
# by: PyQt4 UI code generator 4.9.6
#
# 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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton1 = QtGui.QPushButton(self.centralwidget)
self.pushButton1.setGeometry(QtCore.QRect(50, 40, 75, 23))
self.pushButton1.setObjectName(_fromUtf8("pushButton1"))
self.pushButton2 = QtGui.QPushButton(self.centralwidget)
self.pushButton2.setGeometry(QtCore.QRect(50, 80, 75, 23))
self.pushButton2.setObjectName(_fromUtf8("pushButton2"))
self.pushButton3 = QtGui.QPushButton(self.centralwidget)
self.pushButton3.setGeometry(QtCore.QRect(50, 120, 75, 23))
self.pushButton3.setObjectName(_fromUtf8("pushButton3"))
self.pushButton4 = QtGui.QPushButton(self.centralwidget)
self.pushButton4.setGeometry(QtCore.QRect(50, 160, 75, 23))
self.pushButton4.setObjectName(_fromUtf8("pushButton4"))
self.pushButton5 = QtGui.QPushButton(self.centralwidget)
self.pushButton5.setGeometry(QtCore.QRect(50, 200, 75, 23))
self.pushButton5.setObjectName(_fromUtf8("pushButton5"))
self.pushButton6 = QtGui.QPushButton(self.centralwidget)
self.pushButton6.setGeometry(QtCore.QRect(50, 240, 75, 23))
self.pushButton6.setObjectName(_fromUtf8("pushButton6"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
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.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton1.setText(_translate("MainWindow", "PushButton", None))
self.pushButton2.setText(_translate("MainWindow", "PushButton", None))
self.pushButton3.setText(_translate("MainWindow", "PushButton", None))
self.pushButton4.setText(_translate("MainWindow", "PushButton", None))
self.pushButton5.setText(_translate("MainWindow", "PushButton", None))
self.pushButton6.setText(_translate("MainWindow", "PushButton", None))
和
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'interface_dialog.ui'
#
# Created: Sun Jun 19 12:36:42 2016
# by: PyQt4 UI code generator 4.9.6
#
# 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, 300)
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(60, 50, 46, 13))
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", "TextLabel", None))
您可以在此处下载所有内容:https://drive.google.com/open?id=0B_K-NA5YDW-nc01jZ29wLTd1WE0