Python使用Qt的UI文件

本文介绍如何使用Qt Designer设计PyQt的用户界面,并通过pyuic工具将.ui文件转换为.py文件。此外,还提供了加载UI文件的Python示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

qt的一个强大之后便是可以直观地使用 qt designer 设计界面,pyqt也可以使用这个ui文件,大大简化了ui的设计时间。


使用方法:

如果没有使用eric一类的IDE,那么可以使用命令行的方式将ui文件转化成.py文件,pyqt是自带pyuic.py这个工具的。

首先,必须在给python安装了pyqt,安装方法见http://blog.youkuaiyun.com/lainegates/article/details/8656102

然后,使用Qt Designer设计好ui文件,保存为form.ui,假设路径为D:/

最后,打开cmd,进入pyqt的uic目录,我的是C:\Python26\Lib\site-packages\PyQt4\uic,输入 "python pyuic.py -o D:/ui_form.py D:/form.ui",然后就会在form.ui所在目录生成它对应的.py文件了。


测试载入UI文件:

[python]  view plain copy
  1. class UItest(QtGui.QDialog):  
  2.     def __init__(self,parent=None):  
  3.         QtGui.QWidget.__init__(self,parent)  
  4.         self.loginGui()  
  5.     def loginGui(self):  
  6.         self.ui = Ui_Dialog()  
  7.         self.ui.setupUi(self)  
  8.         self.show()  
  9. app = QtGui.QApplication(sys.argv)  
  10. myqq = UItest()  
  11. sys.exit(app.exec_())  
  12.    


pyuic参数说明:

  1. NAME  

  2.        pyuic4 - compile Qt4 user interfaces to Python code  

  3. SYNOPSIS  

  4.        pyuic4 [OPTION]... FILE  

  5. DESCRIPTION  

  6.        pyuic4  takes  a Qt4 user interface description file and compiles it to  

  7.        Python code. It can also show a preview of the user interface.  

  8. OPTIONS  

  9.        -h, --help  

  10.               Show a summary of the options.  

  11.        --version  

  12.               Display the version number of pyuic4 of the version of Qt  which  

  13.               PyQt4 was generated for.  

  14.        -p, --preview  

  15.               Show a preview of the UI instead of generating Python code.  

  16.        -o, --output=FILE  

  17.               Write the generated Python code to FILE instead of stdout.  

  18.        -d, --debug  

  19.               Show  detailed  debugging  information  about  the UI generation  

  20.               process.  

  21.        -x, --execute  

  22.               Generate extra code to test and display the class when  executed  

  23.               as a script.  

  24.        -i, --indent=NUM  

  25.               Set the indentation width to NUM spaces. A TAB character will be  

  26.               used if NUM is 0 (default: 4).  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值