GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.runoob.com — Python PyQt | 菜鸟教程
Python 3 教程
Python3 教程
Python3 简介
Python3 环境搭建
Python3 Pycharm
Python3 VScode
Python3 基础语法
Python3 基本数据类型
Python3 数据类型转换
Python3 解释器
Python3 注释
Python3 运算符
Python3 数字(Number)
Python3 字符串
Python3 列表
Python3 元组
Python3 字典
Python3 集合
Python3 条件控制
Python3 循环语句
Python3 编程第一步
Python3 推导式
Python3 迭代器与生成器
Python3 with
Python3 函数
Python3 lambda
Python3 装饰器
Python3 数据结构
Python3 模块
Python __name__
Python3 输入和输出
Python3 File
Python3 OS
Python3 错误和异常
Python3 面向对象
Python3 命名空间/作用域
Python 虚拟环境的创建
Python 类型注解
Python3 标准库概览
Python3 实例
Python 测验
Python3 高级教程Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQtPython PyQtPyQt 是一个强大的 Python 库,用于创建图形用户界面(GUI),可以用来代替 Python 内置的 Tkinter。 PyQt 是 Qt 框架的 Python 绑定,广泛应用于桌面应用程序开发。 Qt 是一个跨平台的 C++ 应用程序开发框架。 PyQt 允许 Python 开发者利用 Qt 库创建功能强大的 GUI 应用程序。 PyQt 有以下几个主要版本:
安装 PyQt使用 pip 安装 PyQt5: # 安装PyQt5 pip install PyQt5 # 安装Qt设计师和其他工具(可选) pip install PyQt5-tools 第一个 PyQt 程序创建简单窗口以下是一个最基本的 PyQt 程序,创建一个空白窗口: 实例
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget # 创建应用实例 app = QApplication([]) # 创建主窗口 window = QWidget() window.setWindowTitle("我的第一个 PyQt 程序") window.setGeometry(100, 100, 400, 300) # (x, y, width, height) window.setAttribute(Qt.WA_DeleteOnClose, True) # 销毁 # 显示窗口 window.show() # 运行应用 app.exec_()
代码解析
一个典型的PyQt应用程序包含以下部分:
实例
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton class MainWindow(QMainWindow): def __init__(self): super().__init__() # 设置窗口标题和大小 self.setWindowTitle("我的第一个PyQt应用") self.setGeometry(100, 100, 400, 300) # x, y, width, height # 创建按钮 self.button = QPushButton("点击我", self) self.button.setGeometry(150, 150, 100, 30) self.button.clicked.connect(self.button_clicked) def button_clicked(self): print("按钮被点击了!") if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())
常用 PyQt 组件按钮(QPushButton)实例
from PyQt5.QtWidgets import QPushButton
button = QPushButton("点击我", window) button.move(150, 150) # 设置按钮位置 标签(QLabel)实例
from PyQt5.QtWidgets import QLabel
label = QLabel("Hello PyQt!", window) label.move(100, 100) 文本框(QLineEdit)实例
from PyQt5.QtWidgets import QLineEdit
textbox = QLineEdit(window) textbox.move(100, 50) 更多常用组件内容参考:https://www.runoob.com/python3/python-pyqt-widgets.html 布局管理(QVBoxLayout)使用布局管理器可以自动调整组件位置: 实例
from PyQt5.QtWidgets import QVBoxLayout, QLabel, QPushButton
layout = QVBoxLayout() layout.addWidget(QLabel("用户名")) layout.addWidget(QLineEdit()) layout.addWidget(QPushButton("登录")) window.setLayout(layout) 更多布局管理内容参考:https://www.runoob.com/python3/python-pyqt-layout.html 信号与槽机制PyQt 使用信号(Signal)和槽(Slot)机制处理事件。 PyQt 的信号和槽机制是用于对象之间通信的核心机制。
实例
from PyQt5.QtWidgets import QPushButton
def on_button_click(): print("按钮被点击了!") button = QPushButton("点击我", window) button.clicked.connect(on_button_click) # 连接信号和槽 自定义信号实例
from PyQt5.QtCore import pyqtSignal, QObject
class MyEmitter(QObject): my_signal = pyqtSignal(str) # 定义一个信号 emitter = MyEmitter() emitter.my_signal.connect(lambda x: print(f"收到信号: {x}")) emitter.my_signal.emit("Hello") # 触发信号 更多布局管理内容参考:https://www.runoob.com/python3/python-pyqt-signals-and-slots.html 使用 Qt DesignerQt Designer 是一个可视化设计工具,可以拖放组件来设计界面:
在代码中使用生成的界面: 实例
from PyQt5 import uic # 加载UI文件 Form, Window = uic.loadUiType("output.ui") # 使用UI app = QApplication(sys.argv) window = Window() form = Form() form.setupUi(window) window.show() sys.exit(app.exec_()) 实战:简单的记事本应用界面设计实例
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QTextEdit, QAction, QFileDialog, QMessageBox) class Notepad(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.text_edit = QTextEdit(self) self.setCentralWidget(self.text_edit) self.create_actions() self.create_menus() self.setWindowTitle('简易记事本') self.setGeometry(100, 100, 800, 600) def create_actions(self): # 文件菜单动作 self.new_action = QAction('新建', self) self.new_action.setShortcut('Ctrl+N') self.new_action.triggered.connect(self.new_file) self.open_action = QAction('打开', self) self.open_action.setShortcut('Ctrl+O') self.open_action.triggered.connect(self.open_file) self.save_action = QAction('保存', self) self.save_action.setShortcut('Ctrl+S') self.save_action.triggered.connect(self.save_file) self.exit_action = QAction('退出', self) self.exit_action.setShortcut('Ctrl+Q') self.exit_action.triggered.connect(self.close) # 编辑菜单动作 self.copy_action = QAction('复制', self) self.copy_action.setShortcut('Ctrl+C') self.copy_action.triggered.connect(self.text_edit.copy) self.paste_action = QAction('粘贴', self) self.paste_action.setShortcut('Ctrl+V') self.paste_action.triggered.connect(self.text_edit.paste) self.cut_action = QAction('剪切', self) self.cut_action.setShortcut('Ctrl+X') self.cut_action.triggered.connect(self.text_edit.cut) def create_menus(self): menubar = self.menuBar() # 文件菜单 file_menu = menubar.addMenu('文件') file_menu.addAction(self.new_action) file_menu.addAction(self.open_action) file_menu.addAction(self.save_action) file_menu.addSeparator() file_menu.addAction(self.exit_action) # 编辑菜单 edit_menu = menubar.addMenu('编辑') edit_menu.addAction(self.copy_action) edit_menu.addAction(self.paste_action) edit_menu.addAction(self.cut_action) def new_file(self): self.text_edit.clear() def open_file(self): filename, _ = QFileDialog.getOpenFileName(self, '打开文件') if filename: try: with open(filename, 'r') as f: self.text_edit.setText(f.read()) except Exception as e: QMessageBox.warning(self, '错误', f'无法打开文件: {e}') def save_file(self): filename, _ = QFileDialog.getSaveFileName(self, '保存文件') if filename: try: with open(filename, 'w') as f: f.write(self.text_edit.toPlainText()) except Exception as e: QMessageBox.warning(self, '错误', f'无法保存文件: {e}') if __name__ == '__main__': app = QApplication(sys.argv) notepad = Notepad() notepad.show() sys.exit(app.exec_())
PyQt5 核心组件大多数组件位于 PyQt5.QtWidgets。 高级功能(如多媒体、网络)可能需要其他模块(如 QtCore, QtGui, QtNetwork 等)。 QListView/QTableView/QTreeView 需要搭配数据模型(如 QStandardItemModel)使用,灵活性更高。 其他安装扩展: pip install PyQtWebEngine # 网页支持 pip install PyQtChart # 图表支持
学习资源
AI 思考中...
点我分享笔记取消
分类导航
Advertisement
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |