使用Qt Designer为PySide设计用户界面

使用Qt Designer为PySide设计用户界面 - Sean Lv - 博客园

使用Qt Designer为PySide设计用户界面

Qt Designer中文名:Qt设计师,是一个可视化GUI设计工具。可以帮助我们加快编写Qt程序的速度。Qt Designer可以将设计好的用户界面保存为.ui文件,其实是XML格式的文本文件。

为了在PySide使用.ui文件,我们需要通过工具pyside-uic将.ui文件转换为.py文件,然后将转换后的.py文件引入我们自己的代码中。

下面是一个实际的例子,首先在Qt Designer中设计一个对话框,名为GoToCellDialog,并另存为 gotocelldialog.ui。

gotocelldialog.ui文件内容:

代码
 
      
<? xml version="1.0" encoding="UTF-8" ?>
< ui version ="4.0" >
< class > GoToCellDialog </ class >
< widget class ="QDialog" name ="GoToCellDialog" >
< property name ="geometry" >
< rect >
< x > 0 </ x >
< y > 0 </ y >
< width > 226 </ width >
< height > 70 </ height >
</ rect >
</ property >
< property name ="windowTitle" >
< string > Go to Cell </ string >
</ property >
< widget class ="QLineEdit" name ="lineEdit" >
< property name ="geometry" >
< rect >
< x > 98 </ x >
< y > 8 </ y >
< width > 119 </ width >
< height > 20 </ height >
</ rect >
</ property >
</ widget >
< widget class ="QLabel" name ="label" >
< property name ="geometry" >
< rect >
< x > 8 </ x >
< y > 8 </ y >
< width > 84 </ width >
< height > 20 </ height >
</ rect >
</ property >
< property name ="text" >
< string > &amp; Cell Location: </ string >
</ property >
< property name ="buddy" >
< cstring > lineEdit </ cstring >
</ property >
</ widget >
< widget class ="QPushButton" name ="okButton" >
< property name ="geometry" >
< rect >
< x > 58 </ x >
< y > 38 </ y >
< width > 77 </ width >
< height > 25 </ height >
</ rect >
</ property >
< property name ="text" >
< string > Ok </ string >
</ property >
</ widget >
< widget class ="QPushButton" name ="cancelButton" >
< property name ="geometry" >
< rect >
< x > 141 </ x >
< y > 38 </ y >
< width > 77 </ width >
< height > 25 </ height >
</ rect >
</ property >
< property name ="text" >
< string > Cancel </ string >
</ property >
</ widget >
</ widget >
< resources />
< connections />
</ ui >

 

接下来使用pyside-uic转换.ui文件:

 
     
pyside - uic gotocelldialog.ui - o gotocelldialog_ui.py

 

gotocelldialog_ui.py文件内容:

代码
 
      
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'gotocelldialog.ui'
#
#
Created: Wed Oct 27 17:13:19 2010
#
by: PySide uic UI code generator
#
#
WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_GoToCellDialog(object):
def setupUi(self, GoToCellDialog):
GoToCellDialog.setObjectName(
" GoToCellDialog " )
GoToCellDialog.resize(
226 , 70 )
self.lineEdit
= QtGui.QLineEdit(GoToCellDialog)
self.lineEdit.setGeometry(QtCore.QRect(
98 , 8 , 119 , 20 ))
self.lineEdit.setObjectName(
" lineEdit " )
self.label
= QtGui.QLabel(GoToCellDialog)
self.label.setGeometry(QtCore.QRect(
8 , 8 , 84 , 20 ))
self.label.setObjectName(
" label " )
self.okButton
= QtGui.QPushButton(GoToCellDialog)
self.okButton.setGeometry(QtCore.QRect(
58 , 38 , 77 , 25 ))
self.okButton.setObjectName(
" okButton " )
self.cancelButton
= QtGui.QPushButton(GoToCellDialog)
self.cancelButton.setGeometry(QtCore.QRect(
141 , 38 , 77 , 25 ))
self.cancelButton.setObjectName(
" cancelButton " )
self.label.setBuddy(self.lineEdit)

self.retranslateUi(GoToCellDialog)
QtCore.QMetaObject.connectSlotsByName(GoToCellDialog)

def retranslateUi(self, GoToCellDialog):
GoToCellDialog.setWindowTitle(QtGui.QApplication.translate(
" GoToCellDialog " , " Go to Cell " , None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate(
" GoToCellDialog " , " &Cell Location: " , None, QtGui.QApplication.UnicodeUTF8))
self.okButton.setText(QtGui.QApplication.translate(
" GoToCellDialog " , " Ok " , None, QtGui.QApplication.UnicodeUTF8))
self.cancelButton.setText(QtGui.QApplication.translate(
" GoToCellDialog " , " Cancel " , None, QtGui.QApplication.UnicodeUTF8))

 

然后编写代码,引用gotocelldialog_ui.py,并显示GoToCellDialog。代码如下:

代码
 
      
1 # !/usr/bin/env python
2  
3   import sys
4
5   from PySide.QtCore import *
6   from PySide.QtGui import *
7
8   from gotocelldialog_ui import Ui_GoToCellDialog
9
10   class GoToCellDialog(QDialog):
11 def __init__ (self, parent = None):
12 QDialog. __init__ (self, parent)
13
14 self.ui = Ui_GoToCellDialog()
15 self.ui.setupUi(self)
16
17   def main():
18 app = QApplication(sys.argv)
19 d = GoToCellDialog()
20 d.show()
21 sys.exit(app.exec_())
22
23   if __name__ == ' __main__ ' :
24 main()
复制代码

 

最终的效果图:

好的,我可以为您提供一些关于使用 PySide2 和 Qt Designer 设计 Python 用户登录界面的指导。 首先,您需要安装 PySide2 库和 Qt Designer 工具。可以通过 pip 安装 PySide2,命令如下: ``` pip install PySide2 ``` Qt DesignerQt 开发工具包中的一个可视化界面设计工具,您可以从 Qt 官网下载并安装。 在 Qt Designer设计登录界面后,您需要将 .ui 文件转换为 .py 文件,PySide2 提供一个名为 pyside2-uic 的工具来完成这个转换过程。您可以在终端中使用以下命令将 .ui 文件转换为 .py 文件: ``` pyside2-uic input.ui -o output.py ``` 在 Python 代码中,您需要导入 PySide2 库和转换后的 .py 文件,然后创建登录界面并显示它。以下是一个简单的示例代码: ```python import sys from PySide2.QtWidgets import QApplication, QDialog from output import Ui_LoginDialog class LoginDialog(QDialog, Ui_LoginDialog): def __init__(self): super().__init__() self.setupUi(self) if __name__ == '__main__': app = QApplication(sys.argv) login_dialog = LoginDialog() login_dialog.show() sys.exit(app.exec_()) ``` 在这个示例中,我们导入了 PySide2 库和转换后的 .py 文件,并创建了一个名为 LoginDialog 的 QDialog 子类。我们还在 LoginDialog 类的构造函数中调用了 setupUi() 方法,该方法由 pyside2-uic 工具生成,用于将 .ui 文件中的部件添加到 QDialog 中。 最后,我们创建了一个 QApplication 实例、一个 LoginDialog 实例,并显示了登录界面。最后,我们调用 app.exec_() 方法进入 Qt 事件循环,等待用户与登录界面交互。 希望这些信息可以帮助您开始设计 Python 用户登录界面
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值