将 qtDesigner .ui 文件链接到 python/pyqt?

发布于 2024-08-24 14:41:47 字数 79 浏览 8 评论 0原文

因此,如果我进入 QtDesigner 并构建 UI,它将保存为 .ui 文件。我怎样才能把它作为一个Python文件或在Python中使用它?

So if I go into QtDesigner and build a UI, it'll be saved as a .ui file. How can I make this as a python file or use this in python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(12

放血 2024-08-31 14:41:47

在代码中使用 .ui 的另一种方法是:

from PyQt4 import QtCore, QtGui, uic
class MyWidget(QtGui.QWidget)
    ...
    #somewhere in constructor:
    uic.loadUi('MyWidget.ui', self)

两种方法都很好。不要忘记,如果你使用Qt资源文件(非常有用)作为图标等,你也必须编译它:

pyrcc4.exe -o ui/images_rc.py ui/images/images.qrc

注意,当uic编译界面时,它会在最后添加'import images_rc' .py 文件的名称,因此您必须将资源编译到具有该名称的文件中,或者在生成的代码中重命名。

Another way to use .ui in your code is:

from PyQt4 import QtCore, QtGui, uic
class MyWidget(QtGui.QWidget)
    ...
    #somewhere in constructor:
    uic.loadUi('MyWidget.ui', self)

both approaches are good. Do not forget, that if you use Qt resource files (extremely useful) for icons and so on, you must compile it too:

pyrcc4.exe -o ui/images_rc.py ui/images/images.qrc

Note, when uic compiles interface, it adds 'import images_rc' at the end of .py file, so you must compile resources into the file with this name, or rename it in generated code.

番薯 2024-08-31 14:41:47

结合Max的回答Shriramana Sharma 的邮件列表帖子,我构建了一个小型工作示例,用于加载包含 QMainWindowmywindow.ui 文件> (因此只需选择在 Qt Designer 的 File-New 对话框中创建一个主窗口)。

这是加载它的代码:

import sys
from PyQt4 import QtGui, uic

class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        uic.loadUi('mywindow.ui', self)
        self.show()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    sys.exit(app.exec_())

Combining Max's answer and Shriramana Sharma's mailing list post, I built a small working example for loading a mywindow.ui file containing a QMainWindow (so just choose to create a Main Window in Qt Designer's File-New dialog).

This is the code that loads it:

import sys
from PyQt4 import QtGui, uic

class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        uic.loadUi('mywindow.ui', self)
        self.show()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    sys.exit(app.exec_())
哆兒滾 2024-08-31 14:41:47

您需要使用

pyuic form1.ui > form1.py

pyqt4

pyuic4.bat form1.ui > form1.py

的 pyuic 工具(site-packages\pyqt4\bin)从 ui 文件生成一个 python 文件,然后您可以将 form1 导入到您的脚本中。

You need to generate a python file from your ui file with the pyuic tool (site-packages\pyqt4\bin)

pyuic form1.ui > form1.py

with pyqt4

pyuic4.bat form1.ui > form1.py

Then you can import the form1 into your script.

傾城如夢未必闌珊 2024-08-31 14:41:47

我发现这篇文章非常有帮助。

http://talk.maemo.org/archive/index.php/t-43663.html< /a>

我将简要描述创建 .ui 文件并将其更改为 .py 文件的操作(摘自该文章)。

  1. 从开始菜单启动 Qt Designer。
  2. 创建“主窗口”
  3. 从“新表单”窗口中,从“显示小部件”到左侧“小部件框菜单”底部
    添加“标签小部件”。 (点击拖放)
  4. 双击新添加的Label Widget,将其名称更改为“Hello World”,
  5. 此时您可以使用Control + R热键查看它的外观。
  6. 如果需要,可以通过拖放添加按钮、文本或其他小部件。
  7. 现在保存您的表单..文件->另存为-> “Hello World.ui”(Control + S 也会显示
    “另存为”选项)记下保存“Hello World”.ui 的目录
    文件。 (为了方便起见,我将其保存在 (C:) 中)

文件已创建并保存,现在我们将使用 pyuic 从中生成 Python 代码!

  1. 从开始菜单打开命令窗口。
  2. 现在“cd”到您保存“Hello World.ui”的目录中,对于我来说,我只需
    “cd\”,位于我的“C:>”提示,我的“Hello World.ui”被保存到。
  3. 当您到达存储文件的目录时,键入以下内容。
  4. pyuic4 -x helloworld.ui -o helloworld.py
  5. 恭喜!!你现在有了一个 python Qt4 GUI 应用程序!
  6. 双击 helloworld.py 文件来运行它。 (我使用 pyscripter 并双击
    它在 pyscripter 中打开,然后我从那里“运行”该文件)

希望这对某人有帮助。

I found this article very helpful.

http://talk.maemo.org/archive/index.php/t-43663.html

I'll briefly describe the actions to create and change .ui file to .py file, taken from that article.

  1. Start Qt Designer from your start menu.
  2. From "New Form" window, create "Main Window"
  3. From "Display Widgets" towards the bottom of your "Widget Box Menu" on the left hand side
    add a "Label Widget". (Click Drag and Drop)
  4. Double click on the newly added Label Widget to change its name to "Hello World"
  5. at this point you can use Control + R hotkey to see how it will look.
  6. Add buttons or text or other widgets by drag and drop if you want.
  7. Now save your form.. File->Save As-> "Hello World.ui" (Control + S will also bring up
    the "Save As" option) Keep note of the directory where you saved your "Hello World" .ui
    file. (I saved mine in (C:) for convenience)

The file is created and saved, now we will Generate the Python code from it using pyuic!

  1. From your start menu open a command window.
  2. Now "cd" into the directory where you saved your "Hello World.ui" For me i just had to
    "cd\" and was at my "C:>" prompt, where my "Hello World.ui" was saved to.
  3. When you get to the directory where your file is stored type the following.
  4. pyuic4 -x helloworld.ui -o helloworld.py
  5. Congratulations!! You now have a python Qt4 GUI application!!
  6. Double click your helloworld.py file to run it. ( I use pyscripter and upon double click
    it opens in pyscripter, then i "run" the file from there)

Hope this helps someone.

眼睛会笑 2024-08-31 14:41:47

您还可以通过以下代码在 PyQt5 中使用 uic

from PyQt5 import uic, QtWidgets
import sys

class Ui(QtWidgets.QDialog):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('SomeUi.ui', self)
        self.show()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Ui()
    sys.exit(app.exec_())

You can also use uic in PyQt5 with the following code.

from PyQt5 import uic, QtWidgets
import sys

class Ui(QtWidgets.QDialog):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('SomeUi.ui', self)
        self.show()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Ui()
    sys.exit(app.exec_())
哆兒滾 2024-08-31 14:41:47

我认为更干净的方法是首先像前面提到的那样导出到 .py:

pyuic4 foo.ui > foo.py

然后在代码中使用它(例如 main.py),例如:

from foo import Ui_MyWindow


class MyWindow(QtGui.QDialog):
    def __init__(self):
        super(MyWindow, self).__init__()

        self.ui = Ui_MyWindow()
        self.ui.setupUi(self)

        # go on setting up your handlers like:
        # self.ui.okButton.clicked.connect(function_name)
        # etc...

def main():
    app = QtGui.QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

这种方式为其他不这样做的人提供了能力不要使用 qt-designer 来读取代码,并将功能代码保留在 foo.py 之外,以免被设计器覆盖。您只需通过 MyWindow 类引用 ui 即可,如上所示。

The cleaner way in my opinion is to first export to .py as aforementioned:

pyuic4 foo.ui > foo.py

And then use it inside your code (e.g main.py), like:

from foo import Ui_MyWindow


class MyWindow(QtGui.QDialog):
    def __init__(self):
        super(MyWindow, self).__init__()

        self.ui = Ui_MyWindow()
        self.ui.setupUi(self)

        # go on setting up your handlers like:
        # self.ui.okButton.clicked.connect(function_name)
        # etc...

def main():
    app = QtGui.QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

This way gives the ability to other people who don't use qt-designer to read the code, and also keeps your functionality code outside foo.py that could be overwritten by designer. You just reference ui through MyWindow class as seen above.

江湖彼岸 2024-08-31 14:41:47

您可以使用以下命令将 .ui 文件转换为可执行的 python 文件。

pyuic4 -x form1.ui > form1.py

现在您可以直接执行 python 文件,因为

python3(whatever version) form1.py

您可以导入此文件并可以使用它。

You can convert your .ui files to an executable python file using the below command..

pyuic4 -x form1.ui > form1.py

Now you can straightaway execute the python file as

python3(whatever version) form1.py

You can import this file and you can use it.

臻嫒无言 2024-08-31 14:41:47

你可以像这样编译 ui 文件

pyuic4 -x helloworld.ui -o helloworld.py

you can compile the ui files like this

pyuic4 -x helloworld.ui -o helloworld.py
做个少女永远怀春 2024-08-31 14:41:47

为了将 .ui 文件编译为 .py 文件,我做了

python pyuic.py form1.ui > form1.py

In order to compile .ui files to .py files, I did:

python pyuic.py form1.ui > form1.py

Att.

南巷近海 2024-08-31 14:41:47

在pyqt5中从ui文件转换为.py文件

pyuic5.exe youruifile.ui -o outputpyfile.py -x

in pyqt5 to convert from a ui file to .py file

pyuic5.exe youruifile.ui -o outputpyfile.py -x

策马西风 2024-08-31 14:41:47

2020 年 11 月)这对我有用(UBUNTU 20.04):

pyuic5 /home/someuser/Documents/untitled.ui > /home/someuser/Documents/untitled.py

(November 2020) This worked for me (UBUNTU 20.04):

pyuic5 /home/someuser/Documents/untitled.ui > /home/someuser/Documents/untitled.py
时光清浅 2024-08-31 14:41:47

使用Anaconda3(2018年9月)和QT设计器5.9.5。
在 QT 设计器中,将文件另存为 ui.
打开 Anaconda 提示符。搜索您的文件:cd C:....(复制/粘贴文件的访问路径)。
然后写入: pyuic5 -x helloworld.ui -o helloworld.py (helloworld = 文件名)。进入。
启动 Spyder。打开您的文件 .py。

Using Anaconda3 (September 2018) and QT designer 5.9.5.
In QT designer, save your file as ui.
Open Anaconda prompt. Search for your file: cd C:.... (copy/paste the access path of your file).
Then write: pyuic5 -x helloworld.ui -o helloworld.py (helloworld = name of your file). Enter.
Launch Spyder. Open your file .py.

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