欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python之PySide2安装使用及QT Designer UI设计案例教程

时间:2022-08-14 09:29:37|栏目:Python代码|点击:

pyside2 

>>> pip install pyside2

 QT Designer

>>> pip show pyside2
 
Location: c:\users\Administrator\appdata\roaming\python\python39\site-packages
 
 
文件位置
C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\PySide2\designer.exe

Unable to lanch C: 

PySide2目录下复制uic.exe到新建的bin文件夹

C:\...\Python\Python39\site-packages\PySide2\bin\uic.exe

Ⅰ. Init 

from PySide2.QtWidgets import *
 
class Window:
    def __init__(self):
        self.win = QMainWindow()
 
if __name__ == '__main__':
    app = QApplication([])
    w = Window()
    w.win.show()
    app.exec_()
from PySide2.QtWidgets import *
 
if __name__ == '__main__':
    app = QApplication([])
    win = QMainWindow()
    win.show()
    app.exec_()

Ⅱ Example

from PySide2.QtWidgets import *
 
 
class Window:
    def __init__(self):
        self.win = QMainWindow()
        self.win.resize(500, 400)
        self.win.move(700, 210)
        self.win.setWindowTitle("测试窗口")
 
 
        self.textEdit = QTextEdit(self.win)
        self.textEdit.move(50, 50)
        self.textEdit.resize(200, 300)
        self.textEdit.setPlaceholderText("请输入文本")
 
 
        self.button = QPushButton("button", self.win)
        self.button.move(300, 100)
        self.button.clicked.connect(self.btnClick)
 
 
    def btnClick(self):
        info = self.textEdit.toPlainText()   # 获取文本信息
        print(info)
        # print("button被点击")
 
 
if __name__ == '__main__':
    app = QApplication([])
    # win = QMainWindow()
    # win.show()
    w = Window()
    w.win.show()
    app.exec_()

QT Designer

1.自动生成代码

...
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader
 
    ...
 
if __name__ == "__main__":
    app = QApplication([])
    win = QMainWindow()
    w = Ui_Form()
    w.setupUi(win)
    win.show()
    app.exec_()

2.动态加载ui文件

from PySide2.QtWidgets import *
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader
 
 
class Window:
    def __init__(self):
        super(Window, self).__init__()
 
        # 从文件中加载UI定义
        qfile = QFile("demo1.ui")
        qfile.open(QFile.ReadOnly)
        qfile.close()
 
        # 从UI定义中动态创建一个相应的窗口对象, 将其内部文件定义为self.ui.button
        self.ui = QUiLoader().load(qfile)
 
        self.ui.button.clicked.connect(self.btnClick)
        # self.button.clicked.connect(self.btnClick)
 
    def btnClick(self):
        info = self.ui.textEdit.toPlainText()   # 获取文本信息
        # info = self.textEdit.toPlainText()
        print(info)
 
if __name__ == '__main__':
    app = QApplication([])
    w = Window()
    w.ui.show()
    # w.win.show()
    app.exec_()

上一篇:Python绘制多因子柱状图的实现示例

栏    目:Python代码

下一篇:Python 变量类型详解

本文标题:python之PySide2安装使用及QT Designer UI设计案例教程

本文地址:http://www.codeinn.net/misctech/210741.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有