如何运行pyuic4生成的程序?

发布于 2024-08-13 23:38:59 字数 1341 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您可以将 -x 参数传递给 pyuic。它将生成附加代码以使脚本可执行。


在实际应用程序中,您应该更好地编写 QMainWindow 的子类,如下所示:

# Store this code in the file MyMainWindow.py
from PyQt4.QtGui import *

class MyMainWindow(QMainWindow):
    def __init__(self, ui_layout):
        QMainWindow.__init__(self)

        self.ui = ui_layout
        ui_layout.setupUi(self)

并在与 MyMainWindow.py 相同的目录中创建一个主要可执行脚本:

from PyQt4.QtGui import *
from MyMainWindow import *
from Form1 import *             # replace Form1 the name of your generated file
import sys

app = QApplication(sys.argv)

window = MyMainWindow(Ui_Form1())
window.show()

sys.exit(app.exec_())

然后运行最后一个脚本来启动程序。

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:

# Store this code in the file MyMainWindow.py
from PyQt4.QtGui import *

class MyMainWindow(QMainWindow):
    def __init__(self, ui_layout):
        QMainWindow.__init__(self)

        self.ui = ui_layout
        ui_layout.setupUi(self)

And also create a main executable script in the same directory as MyMainWindow.py:

from PyQt4.QtGui import *
from MyMainWindow import *
from Form1 import *             # replace Form1 the name of your generated file
import sys

app = QApplication(sys.argv)

window = MyMainWindow(Ui_Form1())
window.show()

sys.exit(app.exec_())

Then run this last script to launch the program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文