环境搭建

pip install pyside6 openpyxl pyinstaller pymysql

简单示例

PySide6的窗口类主要有三种,分别为QWidget、QMainWindow和QDialog,其中QMainWindow和QDialog从QWidget类继承而来。要创建和显示窗口,需要用这3个类中的任意一个类实例化对象,并让窗口对象显示并运行起来

import sys
from PySide6.QtWidgets import QApplication, QWidget
app = QApplication(sys.argv)  # M
myWindow = QWidget() # 创建窗口实例对象
myWindow.show() # 显示窗口
n = app.exec() # 执行exec()方法,进入事件循环,若遇到窗口退出命令,返回整数,如果这个整数是0表示正常退出,如果非0表示非正常退出,会停止后续语句的执行,直到所有可视化窗体都关闭(退出)后才执行后续的语句
sys.exit(n) # 通知
import sys
from PySide6. QtWidgets import QApplication,QWidget,QLabel,QPushButton
app = QApplication(sys.argv)
myWindow= QWidget()
myWindow.setWindowTitle('Hello')
myWindow.resize(300,150)
myLabel = QLabel(myWindow)
string = '欢迎使用本书学习编程!'
myLabel. setText(string)
myLabel. setGeometry(80,50,150,20)
myButton = QPushButton(myWindow)
myButton.setText("关闭")
myButton. setGeometry(120,100,50,20)
myButton.clicked. connect(myWindow. close)
myWindow. show()
n = app. exec()
print("n=",n)
try:
  sys. exit(n)
except SystemExit:
  print("请在此做一些其他工作.")

QApplication(sys.argv)

都要创建一个而且只能创建一个QApplication类的实例对象

静态方法

[static]exec()

int

进入消息循环,直到遇到exit()命令退出程序

[static]quit()

Noe

[static]exit(retcode:int=0)

None

退出程序◇exec()的返回值是retcode

[static]setQuitOnLastWindowClosed

设置当最后一个窗口关闭时,程序是否退出,默认是True

None

(bool)

对于接受键盘输入的控件,设置是否自动弹出软件输入面板(software input panel),仅对需要输入面板的系统起作用

setAutoSipEnabled(bool)

None

autoSipEnabled()

bool

获取是否自动弹出软件输入面板

setStyleSheet(sheet:str)

None

设置样式表

styleSheet()

获取样式表

str

[static]activeModalWidget()

QWidget

获取带模式的活跃对话框

[static]activePopupWidget()

QWidget

获取活跃的菜单

[statie]activeWindow()

QWidget

返回能接收键盘输入的顶层窗口

使非活跃的窗口发出预警,并持续指定的时间(毫秒)。如果持续时间为0,则一直发出预警,直到窗口变成活跃的窗口

[static] alert (QWidget, duration:int=0)

None

[static]allWidgets()

List[QWidget]

获取所有的窗口和控件列表

[static]beep()

None

发出铃响

[static]closeAllWindows()

None

关闭所有窗口

[static]cursorFlashTime()

int

获取光标闪烁时间

设置鼠标双击时间(毫秒)以区分双击和两次单击

[static]setDoubleClickInterval(int)

None

获取鼠标双击的时间间隔

[static]doubleClickInterval()

int

[static]focusWidget()

QWidget

获取接收键盘输入的有焦点的控件

[static]setFont(QFont)

None

设置程序默认字体

[static]font()

QFont

获取程序的默认字体

[static]font(QWidget)

QFont

获取指定控件的字体

设置界面特效◇Qt.UIEffect可以取Qt.UI_AnimateMenu、Qt.UI_FadeMenu、Qt.UI_FadeTooltip、Qt.UI_AnimateCombo、Qt.UI_AnimateTooltip、Qt.UI_AnimateToolBox

[static]setEffectEnabled(Qt.

None

UIEffect.enable=True)

[static]isEffectEnabled(Qt.UIEffect)[static]keyboardInputInterval()

bool

获取是否有某种特效

int

获取区分两次键盘输入的时间间隔

[static]palette()

QPalette

获取程序默认的调色板

[static]palette(QWidget)

QPalette

获取指定控件的调色板

[static]setActiveWindow(QWidget)

None

将窗口设置成活跃窗口以响应事件

[static]setCursorFlashTime(int)[static]setEffectEnabled(Qt.UIEffect,enable:bool=True)

None

设置光标闪烁时间(毫秒)

设置界面特效

None

[static]setKeyboardInputInterval(int)

None

设置区分键盘两次输入的时间间隔

QApplication的方法及参数类型

返回值类型

说 明

[static]setPalette (Union [QPalette,

设置程序默认的调色板

None

Qt.GlobalColor.QColor])

设置拖拽动作开始时,光标的移动距离(像素),默认值是10

[static]setStartDragDistance(int)

None

[static]startDragDistance()

int

获取拖拽起始时光标需移动的距离

设置拖拽动作开始时,鼠标按下的时间(毫秒),默认是500

[static]setStartDragTime(ms: int)

None

获取从按下鼠标按键起到拖拽动作开始的时间

[static]startDragTime()

int

[static]setStyle(QStyle)

None

设置程序的风格

[static]style()

QStyle

获取风格

设置转动滚轮时,界面控件移动的行数,默认是3

[static]setWheelScrollLines(int)

None

[static]topLevelAt(QPoint)

QWidget

获取指定位置的顶层窗口

static]topLevelAt(x: int.y: int)

QWidget

获取指定位置的顶层窗口

[static]topLevelWidgets()

List[QWidget]

获取顶层窗口列表,顶层窗口可能被隐藏

[static]widgetAt(QPoint)

QWidget

获取指定位置的窗口

[static]widgetAt(x:int.y:int)

QWidget

获取指定位置的窗口

[static] setApplicationDisplayName

设置程序中所有窗口标题栏上显示的名称

None

(str)

设置程序中的布局方向,Qt.LayoutDirection 可以取Qt. LeftToRight、Qt.RightToLeft或Qt.LayoutDirectionAuto

[static]setLayoutDirection(Qt.

None

LayoutDirection)

[static]setOverrideCursor ( Union [QCursor.Qt.CursorShape.QPixmap])

设置应用程序当前的光标

None

static]overrideCursor()

QCursor

获取当前的光标

恢复setOverrideCursor()之前的光标设置,可以多次恢复

[static]restoreOverrideCursor()

None

[static]setWindowlcon( Union

为整个应用程序设置默认的图标

None

QIcon,QPixmap])

[static]windowIcon()

QIcon

获取图标

[static]setApplicationName(str)

None

设置应用程序名称

[static]set ApplicationVersion(str)

None

设置应用程序的版本

[static]translate(context: bytes,key:bytes.disambiguation: bytes = None.n:int=-1)

str

字符串解码,本地化字符串

[static]postEvent (receiver: QObject.QEvent.priority=Qt.EventPriority)[static]sendEvent(receiver: QObject.QEvent)

将事件放入消息队列的尾端,然后立即返回,不保证事件立即得到处理

None

用notify()函数将事件直接派发给接收者进行处理,返回事件处理情况

bool

QApplication的方法及参数类型

返回值类型

说明

[static] sendPostedEvents (receiver:QObject=None,event_type:int=0)

将事件队列中用postEvent()函数放入的事件立即分发

None

把事件信号发送给接收者,返回接收者的event()函数处理的结果

notify(QObject, QEvent)

bool

event(QEvent)

bool

重写该方法,处理事件

static]sync()

None

处理事件使程序与窗口系统同步

实例方法

QWidget

setWindowTitle()

resize()

setGeometry()

close()

QLabel

setText()

setGeometry()

QPushbutton

setText()

setGeometry()

clicked.connect()

界面与逻辑分离-单继承(ui在一个函数定义,逻辑处理在一个函数定义,逻辑处理包括什么槽和信号的绑定等等)

import sys

from PySide6. QtWidgets import QApplication, QWidget, QLabel, QPushButton
class MyWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi()
        self.button.setText("Close")
        self.button.clicked.connect(self.close)
    def setupUi(self):
        self.setWindowTitle('Hello')
        self.resize(300, 150)
        self.label = QLabel(self)
        self.label.setText('hello')
        self.label.setGeometry(80, 50, 150, 20)
        self.button = QPushButton(self)
        self.button.setText("关闭")
        self.button.setGeometry(120, 100, 50, 20)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = MyWidget()
    myWindow.show()
    sys.exit(app.exec())

PySide6-designer.exe

窗口界面设计工具生成ui文件

转换ui文件为python文件

PySide6-uic.exe

pyside6-uic student.ui -o student.py

python文件Ui_Form类拥有setupUi()函数

使用生成的ui转换得py文件

import sys
from ui import Ui_Form
from PySide6. QtWidgets import QApplication, QWidget, QLabel, QPushButton
class MyWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        Ui_Form().setupUi(self)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = MyWidget()
    myWindow.show()
    sys.exit(app.exec())

Python程序快速转换

import os
ui = 'ui.ui'
py = 'ui.py'
cmdTemplate = f"PySide6-uic.exe {ui} -o {py}"
os.system(cmdTemplate)

containers

共有属性

objectName

group box

title

display widgets

label

text

alignment

input widgets

spin box

readonly

value

maximum

内置信号editingFinshed

buttons

push button

text

内置信号clicked

messagebox

信号与槽

信号与槽的关系可以是一对一,也可以是多对多

内置信号与内置槽的连接

使用Qt Designer工具设置

内置信号与自定义槽函数

自动关联内置信号和自定义槽

自定义的槽函数必须具有如下格式,实现自动关联

from PySide6.QtCore import Slot

@slot()

def on_objectName_signalName (self, signalParameter):

checkBox

定义def on_checkBox_toggled(self,checked)自动关联槽函数,自动接收来自checkBox的toggled(bool)的信号

重载型信号时槽函数前加修饰符@Slot(type)声明是对哪个信号定义槽函数

手动关联内置信号和自定义槽

self.ui.btnCalculate.clicked.connect(self.scoreCalculate)

自定义信号

参数类型需要在定义信号时进行声明

只有继承自QObject的类才可以定义信号。

from PySide6.QtCore import Signal”语句导入Signal类

定义非重载型信号的格式

signalName=Signal (typel, type2,…)

定义重载型信号的格式

signalName=Signal ([type1], [type2],-)

信号有连接connect()、发送emit()和断开disconnect()属性

对于重载型信号,在进行连接、发送和断开时,需要用signalName[type]形式进行连接、发送和断开操作

第1个信号可以不用signalName[type]形式,而直接用signalName形式

控件之间的关系

控件的布局

控件tab键顺序

控件之前的伙伴关系

资源文件

py文件编译打包

pip install pyinstaller
pyinstaller 参数
-D 多个文件,带dll
-F  单个文件
-i  添加图标
--noconsole 不要dos窗口

Python程序快速转换

import os
import time

main = 'main.py'
cmdTemplate = f"pyinstaller -F {main}"
os.system(cmdTemplate)
time.sleep(5)
os.system('start dist\main.exe')

快捷键 ctrl+P ctrl+Q

QSS 编辑器 https://github.com/hustlei/QssStylesheetEditor/releases/download/v1.7/QssStylesheetEditor1.7_win64_Installer.exe

qt-material的使用 列出所有主题 from qt_material import list_themes list_themes() 使用 from qt_material import apply_stylesheet apply_stylesheet(app, theme="light_teal_500.xml")

QtAwesome图标使用 查看 from qtawesome import icon_browser icon_browser.run() 使用 import qtawesome window.ui.clearBtn.setIcon(qtawesome.icon('mdi.cursor-default-click-outline', color='blue')) window.setWindowIcon(qtawesome.icon('fa.cloud-download', color='blue'))

vsc 环境搭建 环境使用解释器ctrl + shift + P: Python: Select Interpreter 安装chiness、python、QT for python、QML Syntax/Tools插件 设置*.qss文件用css关联

results matching ""

    No results matching ""