Skip to content

Commit 08c1219

Browse files
committed
PySide2 使用教程
1 parent 86bbb57 commit 08c1219

14 files changed

+161
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
安装pyside2,需要在绿色版的文件夹右键打开命令行,然后执行下面命令
3+
4+
使用国内镜像加速
5+
6+
```
7+
python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyside2
8+
```
9+
10+
文件分布在好几个目录
11+
12+
在 python-3.7.3-embed-win32\Lib\site-packages 目录里,有3个相关目录
13+
14+
![avatar](./images/1.png)
15+
16+
在Pyside2 文件夹,存放了常用的Qt工具
17+
18+
designer.exe 界面设计器
19+
20+
linguist.exe 多语言工具
21+
22+
23+
![avatar](./images/2.png)
24+
25+
在 python-3.7.3-embed-win32\Scripts 目录里,有 UI 编译工具 pyside2-uic.exe,将 designer 制作好的界面文件 生成对应的 代码文件。
26+
27+
![avatar](./images/3.png)
28+
29+
使用方法
30+
31+
首先使用 designer 制作一个界面
32+
33+
![avatar](./images/4.png)
34+
35+
然后生成代码
36+
37+
```
38+
pyside2-uic.exe C:\Users\crazy\Desktop\test.ui -o C:\Users\crazy\Desktop\test.py
39+
```
40+
41+
![avatar](./images/5.png)
42+
43+
如下内容
44+
45+
![avatar](./images/6.png)
46+
47+
编写代码测试
48+
49+
```
50+
import sys
51+
from PySide2.QtCore import *
52+
from PySide2.QtGui import *
53+
from PySide2.QtWidgets import *
54+
55+
from testDialog import Ui_Dialog
56+
57+
58+
class Test_Form(QDialog):
59+
60+
#构造函数,创建一个新的Dialog
61+
#制作的界面是Dialog,但是生成的代码里面是没有创建Dialog的,只有Dialog上面其他控件的代码,所以要创建一个Dialog
62+
def __new__(cls,*args,**kwargs):
63+
return QDialog.__new__(cls,*args,**kwargs)
64+
65+
def __init__(self,*args,**kwargs):#先执行构造函数,然后执行初始化函数
66+
QDialog.__init__(self,*args,**kwargs)
67+
self.ui= Ui_Dialog()
68+
self.ui.setupUi(self)
69+
70+
if __name__ == "__main__":
71+
# 所有应用必须创建一个应用(Application)对象
72+
app = QApplication(sys.argv)
73+
form = Test_Form()
74+
form.show()
75+
sys.exit(app.exec_())
76+
```
77+
78+
效果
79+
80+
![avatar](./images/7.png)
Binary file not shown.
166 KB
Loading
218 KB
Loading
165 KB
Loading
178 KB
Loading
162 KB
Loading
72.3 KB
Loading
126 KB
Loading
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import sys
2+
from PySide2.QtCore import *
3+
from PySide2.QtGui import *
4+
from PySide2.QtWidgets import *
5+
6+
class Window(QWidget):
7+
def __init__(self):
8+
# 初始化构造用户界面类的基础类,QWidget提供了默认的构造方法
9+
super().__init__()
10+
self.initUI()
11+
12+
def initUI(self):
13+
self.setWindowTitle("网关延迟") # 设置窗口名
14+
self.resize(300, 300) # 设置窗口大小
15+
self.move(500, 200) # 设置窗口位置
16+
self.show() #在屏幕上显示一个widget(窗口)
17+
18+
if __name__ == "__main__":
19+
# 所有应用必须创建一个应用(Application)对象
20+
app = QApplication(sys.argv)
21+
test = Window()
22+
sys.exit(app.exec_())
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Form implementation generated from reading ui file 'E:\test.ui',
4+
# licensing of 'E:\test.ui' applies.
5+
#
6+
# Created: Sat Jun 8 02:04:22 2019
7+
# by: pyside2-uic running on PySide2 5.12.3
8+
#
9+
# WARNING! All changes made in this file will be lost!
10+
11+
from PySide2 import QtCore, QtGui, QtWidgets
12+
13+
class Ui_Dialog(object):
14+
def setupUi(self, Dialog):
15+
Dialog.setObjectName("Dialog")
16+
Dialog.resize(453, 357)
17+
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
18+
self.buttonBox.setGeometry(QtCore.QRect(50, 290, 281, 32))
19+
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
20+
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
21+
self.buttonBox.setObjectName("buttonBox")
22+
self.calendarWidget = QtWidgets.QCalendarWidget(Dialog)
23+
self.calendarWidget.setGeometry(QtCore.QRect(70, 20, 296, 236))
24+
self.calendarWidget.setObjectName("calendarWidget")
25+
26+
self.retranslateUi(Dialog)
27+
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), Dialog.accept)
28+
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
29+
QtCore.QMetaObject.connectSlotsByName(Dialog)
30+
31+
def retranslateUi(self, Dialog):
32+
Dialog.setWindowTitle(QtWidgets.QApplication.translate("Dialog", "Dialog", None, -1))
33+

python-3.7.3-embed-win32/testui.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
from PySide2.QtCore import *
3+
from PySide2.QtGui import *
4+
from PySide2.QtWidgets import *
5+
6+
from testDialog import Ui_Dialog
7+
8+
9+
class Test_Form(QDialog):
10+
11+
#构造函数,创建一个新的Dialog
12+
#制作的界面是Dialog,但是生成的代码里面是没有创建Dialog的,只有Dialog上面其他控件的代码,所以要创建一个Dialog
13+
def __new__(cls,*args,**kwargs):
14+
return QDialog.__new__(cls,*args,**kwargs)
15+
16+
def __init__(self,*args,**kwargs):#先执行构造函数,然后执行初始化函数
17+
QDialog.__init__(self,*args,**kwargs)
18+
self.ui= Ui_Dialog()
19+
self.ui.setupUi(self)
20+
21+
if __name__ == "__main__":
22+
# 所有应用必须创建一个应用(Application)对象
23+
app = QApplication(sys.argv)
24+
form = Test_Form()
25+
form.show()
26+
sys.exit(app.exec_())

0 commit comments

Comments
 (0)