PyQt 简介
包括以下模块:QtCore QtGui QtWidgets QtDBus QtNetwork QtHelp QtXml QtSvg Qtsqll QtTest
PyQt6日期与时间
QDate QTime QDateTime
分别处理公历日期 时间 和组合
PyQt6 有 currentDate
、currentTime
和 currentDateTime
方法获取当前的日期或时间。
…
PyQt6的第一个程序
简单示例
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 import sysfrom PyQt6.QtWidgets import QApplication, QWidgetdef main (): app = QApplication(sys.argv) w = QWidget() w.resize(250 , 200 ) w.move(300 , 300 ) w.setWindowTitle('Simple' ) w.show() sys.exit(app.exec ()) if __name__ == '__main__' : main()
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 """ ZetCode PyQt6 tutorial This example shows a tooltip on a window and a button. Author: Jan Bodnar Website: zetcode.com """ import sysfrom PyQt6.QtWidgets import (QWidget, QToolTip, QPushButton, QApplication) from PyQt6.QtGui import QFontclass Example (QWidget ): def __init__ (self ): super ().__init__() self.initUI() def initUI (self ): QToolTip.setFont(QFont('SansSerif' , 10 )) self.setToolTip('This is a <i>QWidget</i> widget' ) btn = QPushButton('Button' , self) btn.setToolTip('This is a <b>QPushButton</b> widget' ) btn.resize(btn.sizeHint()) btn.move(50 , 50 ) self.setGeometry(300 , 300 , 300 , 200 ) self.setWindowTitle('Tooltips' ) self.show() def main (): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec ()) if __name__ == '__main__' : main()
退出按钮
参数 text 是将显示在按钮上的文本。parent 是我们放置按钮的小部件。在我们的例子中,它将是一个QWidget。应用程序的小部件形成层次结构 ,在这个层次结构中,大多数小部件都有父级。没有父级的小部件的父级是顶级窗口。
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 """ ZetCode PyQt6 tutorial This program creates a quit button. When we press the button, the application terminates. Author: Jan Bodnar Website: zetcode.com """ import sysfrom PyQt6.QtWidgets import QWidget, QPushButton, QApplicationclass Example (QWidget ): def __init__ (self ): super ().__init__() self.initUI() def initUI (self ): qbtn = QPushButton('Quit' , self) qbtn.clicked.connect(QApplication.instance().quit) qbtn.resize(qbtn.sizeHint()) qbtn.move(50 , 50 ) self.setGeometry(300 , 300 , 350 , 250 ) self.setWindowTitle('Quit button' ) self.show() def main (): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec ()) if __name__ == '__main__' : main()
弹窗
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 """ ZetCode PyQt6 tutorial This program shows a confirmation message box when we click on the close button of the application window. Author: Jan Bodnar Website: zetcode.com """ import sysfrom PyQt6.QtWidgets import QWidget, QMessageBox, QApplicationclass Example (QWidget ): def __init__ (self ): super ().__init__() self.initUI() def initUI (self ): self.setGeometry(300 , 300 , 350 , 200 ) self.setWindowTitle('Message box' ) self.show() def closeEvent (self, event ): reply = QMessageBox.question(self, 'Message' , "Are you sure to quit?" , QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, QMessageBox.StandardButton.No) if reply == QMessageBox.StandardButton.Yes: event.accept() else : event.ignore() def main (): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec ()) if __name__ == '__main__' : main()
窗口居中
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 """ ZetCode PyQt6 tutorial This program centers a window on the screen. Author: Jan Bodnar Website: zetcode.com """ import sysfrom PyQt6.QtWidgets import QWidget, QApplicationclass Example (QWidget ): def __init__ (self ): super ().__init__() self.initUI() def initUI (self ): self.resize(350 , 250 ) self.center() self.setWindowTitle('Center' ) self.show() def center (self ): qr = self.frameGeometry() cp = self.screen().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) def main (): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec ()) if __name__ == '__main__' : main()
PyQt6的菜单和工具栏
在这部分教程中,我们创建了一个状态栏、菜单栏和工具栏。菜单是位于菜单栏中的一组命令。工具栏有一些按钮和应用程序中的一些常用命令。状态栏显示状态信息,通常位于应用程序窗口的底部。
QMainWindow
QMainWindow
类提供了主程序窗口。在这里可以创建一个具有状态栏、工具栏和菜单栏的经典应用程序框架。
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 """ ZetCode PyQt6 tutorial This program creates a statusbar. Author: Jan Bodnar Website: zetcode.com """ import sysfrom PyQt6.QtWidgets import QMainWindow, QApplicationclass Example (QMainWindow ): def __init__ (self ): super ().__init__() self.initUI() def initUI (self ): self.statusBar().showMessage('Ready' ) self.setGeometry(300 , 300 , 350 , 250 ) self.setWindowTitle('Statusbar' ) self.show() def main (): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec ()) if __name__ == '__main__' : main()
课程来源: Python GUI Development with PyQt6 & Qt Designer
PyQt Introduction
PyQt6 Installation and First GUI Window
Window Class Types
三种窗口类
QMainWindow:包括工具栏、菜单栏、状态栏等
QDialog: 对话窗口类
QWidget:所有用户界面对象的基类
Adding Icon & Title to PyQT6 Window
Introduction to Qt Designer
1 pyuic6 -x windowUI.ui -o windowUI.py
Working with QLabel
用于显示文字、图像
Working with QPyshButton
QVBoxLayout&QHBoxLayout
QGridLayout 网格布局,用于对齐不见
Event Handling
事件处理机制叫做信号和槽
每个小部件在应用任何事件时(如点击clicked 滑动等)可以发出信号,需要槽连接该信号和方法(method)
很多部件(如按键等)有内置信号,如btn.clicked 通过btn.clicked.connect(self.clicked_btn) (括号内为槽)相连接起来。
Event Handling with Qt Designer
Simple Calculator with Qt Designer
步骤:
确定对象,修改对象命名、属性等
排布对象布局
生成ui文件,并转为py文件
在py文件中编写事件函数(信号、槽)
Working with QRadioButton
单选按钮,一次只能选中一个
Working with QChexbox
复选框
Creating QSpinBox
上下调整的框框以修改值
Creating QDoubleSpinBox