脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 利用pyuic5将ui文件转换为py文件的方法

利用pyuic5将ui文件转换为py文件的方法

2021-07-15 09:59yanwucao Python

今天小编就为大家分享一篇利用pyuic5将ui文件转换为py文件的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

操作系统上正确配置python环境之后,pyuic5也是一个可以识别的命令行指令

到.ui文件的目录下,直接cmd进入,输入pyuic5 -o 转换的py文件 待转换的ui文件

利用pyuic5将ui文件转换为py文件的方法

此时,需要对login.py添加一点代码使得设计好的ui能够出现在我们面前

?
1
2
3
4
5
6
7
8
9
10
import sys
 
 
if __name__ == "__main__":
  app = qtwidgets.qapplication(sys.argv) # 创建一个qapplication,也就是你要开发的软件app
  mainwindow = qtwidgets.qmainwindow()  # 创建一个qmainwindow,用来装载你需要的各种组件、控件
  ui = ui_form()             # ui是你创建的ui类的实例化对象
  ui.setupui(mainwindow)         # 执行类中的setupui方法,方法的参数是第二步中创建的qmainwindow
  mainwindow.show()            # 执行qmainwindow的show()方法,显示这个qmainwindow
  sys.exit(app.exec_())          # 使用exit()或者点击关闭按钮退出qapplication

完整代码段如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
 
# form implementation generated from reading ui file 'login.ui'
#
# created by: pyqt5 ui code generator 5.6
#
# warning! all changes made in this file will be lost!
 
from pyqt5 import qtcore, qtgui, qtwidgets
import sys
 
 
class ui_form(object):
  def setupui(self, form):
    form.setobjectname("form")
    form.resize(400, 300)
    self.pushbutton = qtwidgets.qpushbutton(form)
    self.pushbutton.setgeometry(qtcore.qrect(70, 220, 75, 23))
    self.pushbutton.setobjectname("pushbutton")
    self.pushbutton_2 = qtwidgets.qpushbutton(form)
    self.pushbutton_2.setgeometry(qtcore.qrect(220, 220, 75, 23))
    self.pushbutton_2.setobjectname("pushbutton_2")
    self.checkbox = qtwidgets.qcheckbox(form)
    self.checkbox.setgeometry(qtcore.qrect(70, 180, 141, 16))
    self.checkbox.setobjectname("checkbox")
    self.lineedit = qtwidgets.qlineedit(form)
    self.lineedit.setgeometry(qtcore.qrect(130, 56, 181, 20))
    self.lineedit.setobjectname("lineedit")
    self.lineedit_2 = qtwidgets.qlineedit(form)
    self.lineedit_2.setgeometry(qtcore.qrect(130, 110, 181, 20))
    self.lineedit_2.setobjectname("lineedit_2")
    self.label = qtwidgets.qlabel(form)
    self.label.setgeometry(qtcore.qrect(70, 60, 54, 12))
    self.label.setobjectname("label")
    self.label_2 = qtwidgets.qlabel(form)
    self.label_2.setgeometry(qtcore.qrect(70, 110, 54, 12))
    self.label_2.setobjectname("label_2")
 
    self.retranslateui(form)
    qtcore.qmetaobject.connectslotsbyname(form)
 
  def retranslateui(self, form):
    _translate = qtcore.qcoreapplication.translate
    form.setwindowtitle(_translate("form", "form"))
    self.pushbutton.settext(_translate("form", "取消"))
    self.pushbutton_2.settext(_translate("form", "确定"))
    self.checkbox.settext(_translate("form", "记住用户名和密码"))
    self.label.settext(_translate("form", "用户名:"))
    self.label_2.settext(_translate("form", "密码:"))
 
 
if __name__ == "__main__":
  app = qtwidgets.qapplication(sys.argv) # 创建一个qapplication,也就是你要开发的软件app
  mainwindow = qtwidgets.qmainwindow()  # 创建一个qmainwindow,用来装载你需要的各种组件、控件
  ui = ui_form()             # ui是你创建的ui类的实例化对象
  ui.setupui(mainwindow)         # 执行类中的setupui方法,方法的参数是第二步中创建的qmainwindow
  mainwindow.show()            # 执行qmainwindow的show()方法,显示这个qmainwindow
  sys.exit(app.exec_())          # 使用exit()或者点击关闭按钮退出qapplication

结果显示如下:

利用pyuic5将ui文件转换为py文件的方法

以上这篇利用pyuic5将ui文件转换为py文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/yanwucao/article/details/83302843

延伸 · 阅读

精彩推荐