commit 5613f79151f774918edc7e97f1fcdcffdc055ea4 Author: DC_DC <3284155190@qq.com> Date: Fri Mar 1 14:06:22 2024 +0800 initial commit diff --git a/QT/PyQt/.idea/.gitignore b/QT/PyQt/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/QT/PyQt/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/QT/PyQt/.idea/PyQt.iml b/QT/PyQt/.idea/PyQt.iml new file mode 100644 index 0000000..f571432 --- /dev/null +++ b/QT/PyQt/.idea/PyQt.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/QT/PyQt/.idea/inspectionProfiles/Project_Default.xml b/QT/PyQt/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..1b787d6 --- /dev/null +++ b/QT/PyQt/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,18 @@ + + + + \ No newline at end of file diff --git a/QT/PyQt/.idea/inspectionProfiles/profiles_settings.xml b/QT/PyQt/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/QT/PyQt/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/QT/PyQt/.idea/misc.xml b/QT/PyQt/.idea/misc.xml new file mode 100644 index 0000000..db8786c --- /dev/null +++ b/QT/PyQt/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/QT/PyQt/.idea/modules.xml b/QT/PyQt/.idea/modules.xml new file mode 100644 index 0000000..b32bc4c --- /dev/null +++ b/QT/PyQt/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/QT/PyQt/1. 第一个QT程序.py b/QT/PyQt/1. 第一个QT程序.py new file mode 100644 index 0000000..49d532e --- /dev/null +++ b/QT/PyQt/1. 第一个QT程序.py @@ -0,0 +1,29 @@ +#!D:\Python311\python.exe +# -*- coding: utf-8 -*- +# @Time : 2024/2/26 13:02 +# @Author : DC_DC +"""# 打印版本信息 +from PyQt6.QtCore import PYQT_VERSION_STR, QT_VERSION_STR +print(QT_VERSION_STR) +print(PYQT_VERSION_STR)""" + +import sys # 处理python解释器和系统的交互 +from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel +from PyQt6.QtGui import QIcon # 设置窗口的图标 + + +def main(): + app = QApplication(sys.argv) # 创建一个QApplication对象,用于管理GUI应用程序的控制流和主事件循环 + window = QMainWindow() # 创建一个QMainWindow对象,这是程序的主窗口 + window.setGeometry(500, 500, 300, 200) # 设置窗口的集合属性,即窗口在屏幕上的位置大小 + window.setWindowTitle("DC_DC") # 设置窗口标题 + window.setWindowIcon(QIcon('Photos/img.png')) # 设置窗口图标 + QLabel("Hello, World!", window) # 创建一个QLabel对象,并将其添加到主窗口中 + window.show() # 显示主窗口 + sys.exit(app.exec()) # 调用QApplication对象的exec()方法,进入GUI应用程序的主事件循环,sys.exit()则确保程序在退出事件循环侯正确地退出,并返回一个退出码0 + + +if __name__ == "__main__": + main() + +pass diff --git a/QT/PyQt/2. 居中显示窗口.py b/QT/PyQt/2. 居中显示窗口.py new file mode 100644 index 0000000..f5474bf --- /dev/null +++ b/QT/PyQt/2. 居中显示窗口.py @@ -0,0 +1,32 @@ +#!D:\Python311\python.exe +# -*- coding: utf-8 -*- +# @Time : 2024/2/26 13:34 +# @Author : DC_DC +import sys +from PyQt6.QtWidgets import QApplication, QWidget, QLabel +from PyQt6.QtGui import QGuiApplication +from PyQt6.QtGui import QIcon + + +class MyApplication(QWidget): + def __init__(self): + super().__init__() + self.init_ui() + + def init_ui(self): + self.setGeometry(0, 0, 300, 200) + self.setWindowIcon(QIcon('Photos/img.png')) + qr = self.frameGeometry() # 创建一个矩形对象qr,它表示窗口的几何形状 + + cp = QGuiApplication.primaryScreen().availableGeometry().center() # 获取主屏幕的几何属性,计算出屏幕中心点的坐标 + qr.moveCenter(cp) # 将窗口的几何形状移动到屏幕中心 + self.move(qr.topLeft()) # 将窗口移动到矩形的左上角,以实现窗口剧中显示的效果 + + QLabel('Hello World!', self) + + +if __name__ == '__main__': + app = QApplication(sys.argv) + ex = MyApplication() + ex.show() # 调用父类方法,显示窗口 + sys.exit(app.exec()) diff --git a/QT/PyQt/3. QMainWindow主要内容.py b/QT/PyQt/3. QMainWindow主要内容.py new file mode 100644 index 0000000..bdd97a8 --- /dev/null +++ b/QT/PyQt/3. QMainWindow主要内容.py @@ -0,0 +1,54 @@ +#!D:\Python311\python.exe +# -*- coding: utf-8 -*- +# @Time : 2024/2/26 14:01 +# @Author : DC_DC +""" +QMainWindow是QT框架中的一个类,用于创建主窗口应用程序,提供一个具有一般应用程序框架的主窗口,包括菜单栏、工具栏、状态栏和中央工作区域 +使用这个类可以管理主窗口 +1. 主窗口标题 +2. 主窗口标签 +3. 菜单栏 +4. 工具栏 +5. 状态栏 +""" +import sys + +from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel + + +class MyMainWindow(QMainWindow): + def __init__(self): + super().__init__() + # 设置主窗口标题 + self.setWindowTitle('My Main Window') + # 添加标签到中央工作区域 + central_widget = QLabel("Hello, QMainWindow!") + self.setCentralWidget(central_widget) + # 添加菜单栏 + menubar = self.menuBar() + file_menu = menubar.addMenu('File') + file_menu.addAction('Open') + file_menu.addAction('Save') + # 添加工具栏 + toolbar = self.addToolBar('Tools') + toolbar.addAction('Cut') + toolbar.addAction('Copy') + toolbar.addAction('Paste') + # 添加状态栏 + statusbar = self.statusBar() + statusbar.showMessage('Ready') + + +if __name__ == '__main__': + app = QApplication([]) # 这种写法和下面的写法一致 + """ + 为什么要实例化这个类呢? + 1. 管理应用程序的事件循环 + 2. 管理应用程序的全局状态 + 3,处理命令行参数 + 记住,创建一个QApplication对象是整个PyQT5应用程序的入口点 + """ + # app = QApplication(sys.argv if sys.argv else []) + window = MyMainWindow() + window.show() + app.exec_() diff --git a/QT/PyQt/4. 多页面切换实例.py b/QT/PyQt/4. 多页面切换实例.py new file mode 100644 index 0000000..6dce6ad --- /dev/null +++ b/QT/PyQt/4. 多页面切换实例.py @@ -0,0 +1,40 @@ +#!D:\Python311\python.exe +# -*- coding: utf-8 -*- +# @Time : 2024/2/28 16:09 +# @Author : DC_DC +import sys +from PyQt6.QtWidgets import QApplication, QMainWindow, QTabWidget, QVBoxLayout, QPushButton, QWidget, QHBoxLayout +from PyQt6.QtGui import QIcon + + +class MainWindow(QMainWindow): + def __init__(self): + super().__init__() + self.setWindowTitle("DC_DC") # 设置窗口标题 + self.setWindowIcon(QIcon('Photos/img.png')) # 设置图标 + self.setGeometry(100, 100, 800, 600) # 设置窗口大小 + self.tab_widget = QTabWidget() # 创建了一个QTabWidget实例,用于容纳标签页。 + self.setCentralWidget(self.tab_widget) # 设置标签页空间位置 + self.page1 = QWidget() # 创建页面1 + self.page2 = QWidget() # 创建页面2 + self.tab_widget.addTab(self.page1, "页面1") # 添加页面1到QTabWidget + self.tab_widget.addTab(self.page2, "页面2") # 添加页面2到QTabWidget + layout = QVBoxLayout() # 定义一个垂直布局对象,用来组织两个按钮 如果设置水平就实例化QHBoxLayout 一个布局对象只能被设置给一个父对象 + button1 = QPushButton("切换到页面1") # 定义一个按钮 + button1.clicked.connect(lambda: self.switch_page(1)) # 设置按钮的映射关系 + button2 = QPushButton("切换到页面2") # 定义一个按钮 + button2.clicked.connect(lambda: self.switch_page(2)) # 设置按钮的映射关系 + layout.addWidget(button1) # 添加按钮1到垂直布局对象 + layout.addWidget(button2) # 添加按钮2到垂直布局对象 + self.page2.setLayout(layout) # 按照布局规则进行摆放页面 + self.page1.setLayout(layout) # 按照布局规则进行摆放页面 + + def switch_page(self, index): + self.tab_widget.setCurrentIndex(index - 1) + + +if __name__ == "__main__": + app = QApplication([]) # 创建一个应用程序对象 + mainWin = MainWindow() + mainWin.show() + sys.exit(app.exec()) diff --git a/QT/PyQt/Photos/img.png b/QT/PyQt/Photos/img.png new file mode 100644 index 0000000..7c98350 Binary files /dev/null and b/QT/PyQt/Photos/img.png differ