如何运行pyuic4生成的程序?
我是 PyQt 的新手,虽然我对 python 有点了解。我想要 Qt 设计器进行 GUI 编程,因为它会让我的工作更轻松。我在 Qt 设计器中创建了一个简单的对话框,并使用 pyuic4 进行了转换。
from PyQt4 import QtCore, QtGui
class Ui_Form1(object):
def setupUi(self, Form1):
Form1.setObjectName("Form1")
Form1.resize(495, 364)
self.listWidget = QtGui.QListWidget(Form1)
self.listWidget.setGeometry(QtCore.QRect(60, 100, 221, 111))
self.listWidget.setObjectName("listWidget")
self.lineEdit = QtGui.QLineEdit(Form1)
self.lineEdit.setGeometry(QtCore.QRect(60, 250, 221, 26))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtGui.QPushButton(Form1)
self.pushButton.setGeometry(QtCore.QRect(350, 170, 92, 28))
self.pushButton.setAutoDefault(False)
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form1)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.listWidget.clear)
QtCore.QMetaObject.connectSlotsByName(Form1)
def retranslateUi(self, Form1):
Form1.setWindowTitle(QtGui.QApplication.translate("Form1", "Form1", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Form1", "X", None, QtGui.QApplication.UnicodeUTF8))
我想运行这个程序。如何通过导入该文件来运行该程序?我知道这是一个非常基本的问题。
I'm new to PyQt though I know python a bit.. I wanted to Qt designer for GUI programming since it'll make my job easier. I've taken a simple dialog in Qt designer and converted using pyuic4.
from PyQt4 import QtCore, QtGui
class Ui_Form1(object):
def setupUi(self, Form1):
Form1.setObjectName("Form1")
Form1.resize(495, 364)
self.listWidget = QtGui.QListWidget(Form1)
self.listWidget.setGeometry(QtCore.QRect(60, 100, 221, 111))
self.listWidget.setObjectName("listWidget")
self.lineEdit = QtGui.QLineEdit(Form1)
self.lineEdit.setGeometry(QtCore.QRect(60, 250, 221, 26))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtGui.QPushButton(Form1)
self.pushButton.setGeometry(QtCore.QRect(350, 170, 92, 28))
self.pushButton.setAutoDefault(False)
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form1)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.listWidget.clear)
QtCore.QMetaObject.connectSlotsByName(Form1)
def retranslateUi(self, Form1):
Form1.setWindowTitle(QtGui.QApplication.translate("Form1", "Form1", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Form1", "X", None, QtGui.QApplication.UnicodeUTF8))
I want to run this program. How to run this program from this file by importing this? I know it's a very basic question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
-x
参数传递给 pyuic。它将生成附加代码以使脚本可执行。在实际应用程序中,您应该更好地编写
QMainWindow
的子类,如下所示:并在与 MyMainWindow.py 相同的目录中创建一个主要可执行脚本:
然后运行最后一个脚本来启动程序。
You may pass
-x
parameter to pyuic. It will generate addtional code to make the script executable.In real application you should better write a subclass of
QMainWindow
which could look like this:And also create a main executable script in the same directory as MyMainWindow.py:
Then run this last script to launch the program.