如何从 pyuic .py 文件重建 .ui 文件 [英] How to reconstruct a .ui file from pyuic .py file

查看:106
本文介绍了如何从 pyuic .py 文件重建 .ui 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不久前我使用 PyQt 做了一个项目.我创建了一些 .ui 文件并使用 pyuic4 生成了相应的 .py 文件.我想重新开始工作,但我丢失了 .ui 文件(我格式化了我的电脑并进行了备份,但 .ui 文件驻留在 Qt 设计器文件夹中并丢失了).

A while back I made a project using PyQt. I created some .ui files and generated the corresponding .py files using pyuic4. I want to start work on it again, but I have lost the .ui files (I formatted my PC and took a backup, but the .ui files were residing in the Qt designer folder and got lost).

有什么办法可以从生成的 .py 文件中恢复那些 .ui 文件?

Is there any way I can restore those .ui files from the .py files generated?

推荐答案

可以使用 QFormBuilder:

from PyQt4 import QtCore, QtGui, QtDesigner
from myui import Ui_Dialog

def dump_ui(widget, path):
    builder = QtDesigner.QFormBuilder()
    stream = QtCore.QFile(path)
    stream.open(QtCore.QIODevice.WriteOnly)
    builder.save(stream, widget)
    stream.close()

app = QtGui.QApplication([''])

dialog = QtGui.QDialog()
Ui_Dialog().setupUi(dialog)

dialog.show()

dump_ui(dialog, 'myui.ui')

(注意:为了获得最佳结果,显示窗口似乎非常重要).

(NB: showing the window seems to be quite important in order to get the best results).

不过,不要指望能完美地重建原始 ui 文件.您几乎肯定需要进行大量整理才能获得可接受的内容 - 但如果您的用户界面非常复杂,那么它应该仍然值得.

Don't expect to get a perfect reconstruction of your original ui file, though. You will almost certainly need to do quite a lot of tidying up to get something acceptable - but if your ui is quite complex, it should still be worth it.

这篇关于如何从 pyuic .py 文件重建 .ui 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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