我使用PyQt4设计器设计我的GUI应用程序,然后将.ui文件转换为.py文件。我试图运行.py文件,但没有打开窗口。
.py文件有什么问题?
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(QtGui.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(460, 260)
MainWindow.setMinimumSize(QtCore.QSize(460, 260))
MainWindow.setMaximumSize(QtCore.QSize(460, 260))
MainWindow.setBaseSize(QtCore.QSize(300, 100))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 0, 451, 234))
self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(-1, 5, -1, -1)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.textBrowser = QtGui.QTextBrowser(self.gridLayoutWidget)
self.textBrowser.setObjectName(_fromUtf8("textBrowser"))
self.gridLayout.addWidget(self.textBrowser, 3, 2, 1, 1)
self.pushButton_2 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_2.setFlat(False)
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.gridLayout.addWidget(self.pushButton_2, 4, 2, 1, 1)
self.label_3 = QtGui.QLabel(self.gridLayoutWidget)
self.label_3.setIndent(5)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)
self.lineEdit = QtGui.QLineEdit(self.gridLayoutWidget)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.gridLayout.addWidget(self.lineEdit, 0, 2, 1, 1)
self.label = QtGui.QLabel(self.gridLayoutWidget)
self.label.setIndent(5)
self.label.setObjectName(_fromUtf8("label"))
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.label_2 = QtGui.QLabel(self.gridLayoutWidget)
self.label_2.setIndent(5)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
self.lineEdit_2 = QtGui.QLineEdit(self.gridLayoutWidget)
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.gridLayout.addWidget(self.lineEdit_2, 1, 2, 1, 1)
self.pushButton = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.gridLayout.addWidget(self.pushButton, 1, 3, 1, 1)
self.lineEdit_3 = QtGui.QLineEdit(self.gridLayoutWidget)
self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3"))
self.gridLayout.addWidget(self.lineEdit_3, 2, 2, 1, 1)
self.pushButton_3 = QtGui.QPushButton(self.gridLayoutWidget)
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.gridLayout.addWidget(self.pushButton_3, 2, 3, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 460, 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", "Email Find", None))
self.pushButton_2.setText(_translate("MainWindow", "Find", None))
self.label_3.setText(_translate("MainWindow", "Emails File", None))
self.label.setText(_translate("MainWindow", "URL", None))
self.label_2.setText(_translate("MainWindow", "File", None))
self.pushButton.setText(_translate("MainWindow", "Browse", None))
self.pushButton_3.setText(_translate("MainWindow", "Browse", None))
答案 0 :(得分:1)
pyuic4
(与常规Qt uic
类似)不会为您提供直接运行以获取窗口的文件,它只会生成一个构建“UI内容”的类MainWindow
(或其他类似窗口的类型),您将继承添加自己的东西;我们的想法是你想要对窗口进行自定义(事件处理程序等),同时委托给这个辅助类构建设计器绘制的UI的“无聊”部分。
我们说pyuic4
生成了您的ui_mainwindow.py
文件;在您的mainwindow.py
中,您将拥有类似的内容:
import sys
from ui_mainwindow import Ui_MainWindow
from PyQt4.QtGui import QApplication
class MainWindow(Ui_MainWindow):
def __init__(self, parent = None):
Ui_MainWindow.__init__(self, parent)
self.setupUi(self)
# add your event handlers, extra code, ...
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
app.exec_()