你如何在python中执行pyqt .ui代码? [英] How do you execute pyqt .ui code in python?

查看:60
本文介绍了你如何在python中执行pyqt .ui代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我正在制作的脚本制作 GUI,经过研究后我发现 pyqt 会有所帮助.我现在已经在 pyqt 中制作了一个没有工作按钮的 GUI,但想看看代码.

I am making a GUI for a script I am making, after researching I found pyqt would be helpful. I have now made a GUI with no working buttons as of yet in pyqt but wanted to look at the code.

我打开 cmd.exe 并输入:

I opened cmd.exe and typed in:

pyuic4 project.ui -o python.py

它为我创建了一个 python.py 脚本,它显示了我需要的所有脚本,但是当我通过 IDLE 或命令提示符运行它时,它显示它正在运行,但没有打开 GUI.我做错了什么吗?

It created me a python.py script which showed me all the script I needed, however when I run it via IDLE or command prompt it shows it as running but no GUI opens up. Am I doing something wrong?

另外,pyqt 适合用于为 Windows 创建简单的 GUI,还是适合用于移动开发?

Also on a side note, is pyqt suitable to use to create a simple GUI for windows, or is it for mobile development?

这是我想编译和运行的一段代码

Here is a snippet of code that i want to compile and run

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_FileHash(object):
    def setupUi(self, FileHash):

推荐答案

查看本教程关于如何使用pyuic4命令生成的代码;

Check this tutorial on how to use the generated code by the pyuic4 command;

基本上,您需要这样做:

Basically, you need to do this:

import sys
from PyQt4.QtGui import QApplication, QDialog
from ui_project import Ui_Name  # here you need to correct the names

app = QApplication(sys.argv)
window = QDialog()
ui = Ui_Name()
ui.setupUi(window)

window.show()
sys.exit(app.exec_())

Pyqt 完全适合在 Windows 上创建 GUI.

Pyqt is completely suitable to create GUIs on Windows.

这篇关于你如何在python中执行pyqt .ui代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆