我刚开始使用PyQt,我想知道如何才能通过按下按钮连接在PyQt4 Designer上制作的两个窗口?例如,当我按下按钮时,新窗口将在屏幕上弹出。我发现的解决方案(与建议的解决方案一样)都涉及一种__init__
方法,但是当我将.ui转换为.py文件时,没有__init__
函数被实现。
附上我要连接的两个代码: 代码1:
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):
'''a bunch of initialization of widgets and buttons'''
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow",
None))
self.label.setText(_translate("MainWindow", "Preferences", None))
self.newbtn.setText(_translate("MainWindow", "Add New", None))
self.label_2.setText(_translate("MainWindow", "Current Units: US", None))
self.pushButton.setText(_translate("MainWindow", "US", None))
self.pushButton_2.setText(_translate("MainWindow", "Metric", None))
self.label_5.setText(_translate("MainWindow", "List of Devices", None))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "High", None))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("MainWindow", "Low", None))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
item = self.tableWidget.item(0, 0)
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_())
代码2: 从PyQt4导入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(402, 300)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
'''a lot of initialization stuff'''
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.label.setText(_translate("Dialog", "SN:", None))
self.label_2.setText(_translate("Dialog", "Name:", None))
self.label_3.setText(_translate("Dialog", "Register New Device", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())