Window 使用 qt 设计器构建的未显示
Window built with qt designer not showing up
我正在尝试学习如何使用 pyqt 构建uild 一些简单的 gui 东西,并且我制作了一个带有标签、行编辑和按钮的对话框 window在 Qt 设计器中并将 ui 文件转换为 python 代码。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'gui.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# 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(569, 444)
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(90, 90, 46, 13))
self.label.setObjectName(_fromUtf8("label"))
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(160, 90, 113, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(170, 160, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.lineEdit.clear)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.label.setText(_translate("Dialog", "TextLabel", None))
self.pushButton.setText(_translate("Dialog", "PushButton", None))
这是导入 gui 文件的文件中的代码
import sys
from gui import *
class MyForm(QtGui.QDialog):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
if __name__ == "main":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
当我尝试 运行 它与 python.exe window 瞬间弹出并消失。我正在使用 python 3.4 和 Qt 4.1.1.4 32 位
问题出在您的代码中 'main'
的拼写错误,应该是:
if __name__ == "__main__": #main with two underscores on both sides
我正在尝试学习如何使用 pyqt 构建uild 一些简单的 gui 东西,并且我制作了一个带有标签、行编辑和按钮的对话框 window在 Qt 设计器中并将 ui 文件转换为 python 代码。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'gui.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# 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(569, 444)
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(90, 90, 46, 13))
self.label.setObjectName(_fromUtf8("label"))
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(160, 90, 113, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(170, 160, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.lineEdit.clear)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.label.setText(_translate("Dialog", "TextLabel", None))
self.pushButton.setText(_translate("Dialog", "PushButton", None))
这是导入 gui 文件的文件中的代码
import sys
from gui import *
class MyForm(QtGui.QDialog):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
if __name__ == "main":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
当我尝试 运行 它与 python.exe window 瞬间弹出并消失。我正在使用 python 3.4 和 Qt 4.1.1.4 32 位
问题出在您的代码中 'main'
的拼写错误,应该是:
if __name__ == "__main__": #main with two underscores on both sides