指出:
我开始学习pyqt并通过qt desiger使用pyqt4。
我打算做一些测试项目并将其导出为.py文件。
可悲的是,当我运行脚本时没有显示任何内容,我想这是因为脚本没有.show()
命令所以所有内容都“打印”在内存中但它永远不会显示。可悲的是,我无法弄清楚应该把它放在哪里让事情发挥作用。
代码:
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication(sys.argv)
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(576, 431)
MainWindow.setAccessibleName(_fromUtf8(""))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setAccessibleName(_fromUtf8(""))
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.rAgent = QtGui.QRadioButton(self.centralwidget)
self.rAgent.setGeometry(QtCore.QRect(20, 40, 114, 22))
self.rAgent.setAccessibleName(_fromUtf8(""))
self.rAgent.setObjectName(_fromUtf8("rAgent"))
self.rDomek = QtGui.QRadioButton(self.centralwidget)
self.rDomek.setGeometry(QtCore.QRect(100, 40, 114, 22))
self.rDomek.setAccessibleName(_fromUtf8(""))
self.rDomek.setObjectName(_fromUtf8("rDomek"))
self.rWilk = QtGui.QRadioButton(self.centralwidget)
self.rWilk.setGeometry(QtCore.QRect(220, 40, 114, 22))
self.rWilk.setAccessibleName(_fromUtf8(""))
self.rWilk.setObjectName(_fromUtf8("rWilk"))
self.r4 = QtGui.QRadioButton(self.centralwidget)
self.r4.setGeometry(QtCore.QRect(330, 40, 114, 22))
self.r4.setAccessibleName(_fromUtf8(""))
self.r4.setObjectName(_fromUtf8("r4"))
self.r5 = QtGui.QRadioButton(self.centralwidget)
self.r5.setGeometry(QtCore.QRect(450, 40, 114, 22))
self.r5.setAccessibleName(_fromUtf8(""))
self.r5.setObjectName(_fromUtf8("r5"))
self.lScenario = QtGui.QLabel(self.centralwidget)
self.lScenario.setGeometry(QtCore.QRect(20, 10, 231, 17))
self.lScenario.setAccessibleName(_fromUtf8(""))
self.lScenario.setObjectName(_fromUtf8("lScenario"))
self.tCzas = QtGui.QLineEdit(self.centralwidget)
self.tCzas.setGeometry(QtCore.QRect(20, 100, 41, 29))
self.tCzas.setAccessibleName(_fromUtf8(""))
self.tCzas.setText(_fromUtf8(""))
self.tCzas.setObjectName(_fromUtf8("tCzas"))
self.tID = QtGui.QLineEdit(self.centralwidget)
self.tID.setGeometry(QtCore.QRect(90, 100, 113, 29))
self.tID.setAccessibleName(_fromUtf8(""))
self.tID.setInputMethodHints(QtCore.Qt.ImhDigitsOnly|QtCore.Qt.ImhFormattedNumbersOnly)
self.tID.setText(_fromUtf8(""))
self.tID.setObjectName(_fromUtf8("tID"))
self.tTeam = QtGui.QLineEdit(self.centralwidget)
self.tTeam.setGeometry(QtCore.QRect(230, 100, 171, 29))
self.tTeam.setAccessibleName(_fromUtf8(""))
self.tTeam.setText(_fromUtf8(""))
self.tTeam.setObjectName(_fromUtf8("tTeam"))
self.cFacebook = QtGui.QCheckBox(self.centralwidget)
self.cFacebook.setGeometry(QtCore.QRect(50, 190, 95, 22))
self.cFacebook.setAccessibleName(_fromUtf8(""))
self.cFacebook.setObjectName(_fromUtf8("cFacebook"))
self.buttonBox = QtGui.QDialogButtonBox(self.centralwidget)
self.buttonBox.setGeometry(QtCore.QRect(370, 380, 176, 27))
self.buttonBox.setAccessibleName(_fromUtf8(""))
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.cMail = QtGui.QCheckBox(self.centralwidget)
self.cMail.setGeometry(QtCore.QRect(50, 230, 95, 22))
self.cMail.setAccessibleName(_fromUtf8(""))
self.cMail.setObjectName(_fromUtf8("cMail"))
self.tMail = QtGui.QPlainTextEdit(self.centralwidget)
self.tMail.setGeometry(QtCore.QRect(50, 270, 451, 75))
self.tMail.setAccessibleName(_fromUtf8(""))
self.tMail.setInputMethodHints(QtCore.Qt.ImhNone)
self.tMail.setPlainText(_fromUtf8(""))
self.tMail.setOverwriteMode(False)
self.tMail.setObjectName(_fromUtf8("tMail"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusBar = QtGui.QStatusBar(MainWindow)
self.statusBar.setAccessibleName(_fromUtf8(""))
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.rAgent.setText(_translate("MainWindow", "Agent", None))
self.rDomek.setText(_translate("MainWindow", "Dom w lesie", None))
self.lScenario.setText(_translate("MainWindow", "Wybierz Scenariusz:", None))
self.rWilk.setText(_translate("MainWindow", "Biały Wilk", None))
self.r4.setText(_translate("MainWindow", "Scenario #4", None))
self.r5.setText(_translate("MainWindow", "Scenario #5", None))
self.cFacebook.setText(_translate("MainWindow", "facebook", None))
self.cMail.setText(_translate("MainWindow", "mail", None))
#if rAgent.isChecked():
# print (buttonname + "checked")
答案 0 :(得分:0)
如果你想使用导出为python文件的Qt Designer文件,你应该导入该文件,然后你可以按如下方式使用它:
from yourfilename import Ui_MainWindow
ui = Ui_MainWindow()
ui.setupUi() # needed to setup the ui you defined in the designer
ui.show()
您还可以使用自己的子类扩展Ui_MainWindow类。在这种情况下,您需要调用类似self.setupUi(self)
的内容。
答案 1 :(得分:0)
你应该可以将它添加到你的班级:
class Ui_MainWindow(object):
def __init__(self):
self.setupUi()
self.show()
当在下面的语句中初始化类时,将调用此方法。此代码应低于您的所有代码。
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Ui_MainWindow()
sys.exit(app.exec_())
执行脚本时会自动调用此方法。