一文掌握python图形界面开发PyQt6--(二)定时器、图像处理、菜单和工具栏、自定义控件、多线程、拖放操作、样式和主题

 目录

 

10. 定时器

11. 图像处理

11.1 使用 QImage 加载和显示图像

11.2 使用 QPixmap 加载和显示图像

11.3 使用 QLabel 的 setScaledContents 方法自动缩放图像

11.4 手动调整 QPixmap 的大小

12. 创建菜单和工具栏

12.1 创建菜单

12.2 创建工具栏

12.3 同时创建菜单和工具栏

12.4 创建下拉菜单

13. 自定义控件

13.1 自定义 QWidget

13.2 自定义事件

14. 多线程与 PyQt6

14.1 使用 QThread

14.2 使用 QTimer 和 QThread 合作

15. 拖放操作

15.1 启用拖放支持

15.2 拖放文件

16. 样式和主题

16.1 应用样式表 (QSS)

1)局部样式表

2)全局样式表

16.2 更改主题

1)样式表定制主题

2) 还可以利用 PyQt6 支持的 QPalette 类来定制应用的颜色主题


10. 定时器

使用 QTimer 可以创建定时器,实现定时事件。

import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt6.QtCore import QTimer, QDateTime


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

        # 设置定时器
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update)
        self.timer.start(1000)  # 每 1 秒触发一次

    def initUI(self):
        # 初始化界面
        self.label = QLabel('等待更新...', self)

        layout = QVBoxLayout()
        layout.addWidget(self.label)
        self.setLayout(layout)

        self.setWindowTitle('定时器示例')
        self.show()

    def update(self):
        # 定时器触发时调用的方法
        current_time = QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss')
        self.label.setText(f'当前时间: {current_time}')
        print(f"定时器触发 - {current_time}")


if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = MainWindow()

    sys.exit(app.exec())

每秒会更新一次

11. 图像处理

QPixmapQImage 类可以用于加载和显示图像。

11.1 使用 QImage 加载和显示图像

QImage 可以直接从文件加载图像,并提供了一些图像处理功能。

import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt6.QtGui import QImage, QPixmap


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        # 加载图像
        image = QImage("deng.png")

        # 将 QImage 转换为 QPixmap
        pixmap = QPixmap.fromImage(image)

        # 创建一个 QLabel 并设置其内容为 QPixmap
        label = QLabel(self)
        label.setPixmap(pixmap)

        # 设置布局
        layout = QVBoxLayout()
        layout.addWidget(label)
        self.setLayout(layout)

        self.setWindowTitle('QImage 示例')
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = MainWindow()

    sys.exit(app.exec())

11.2 使用 QPixmap 加载和显示图像

QPixmap 是专门为 GUI 应用程序设计的,更适合用于显示图像。

import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt6.QtGui import QPixmap


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        # 加载图像
        pixmap = QPixmap("deng.png")

        # 创建一个 QLabel 并设置其内容为 QPixmap
        label = QLabel(self)
        label.setPixmap(pixmap)

        # 设置布局
        layout = QVBoxLayout()
        layout.addWidget(label)
        self.setLayout(layout)

        self.setWindowTitle('QPixmap 示例')
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = MainWindow()

    sys.exit(app.exec())

11.3 使用 QLabel 的 setScaledContents 方法自动缩放图像

import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt6.QtGui import QPixmap


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        # 加载图像
        pixmap = QPixmap("deng.png")

        # 创建一个 QLabel 并设置其内容为 QPixmap
        label = QLabel(self)
        label.setPixmap(pixmap)
        label.setScaledContents(True)  # 自动缩放图像

        # 设置布局
        layout = QVBoxLayout()
        layout.addWidget(label)
        self.setLayout(layout)

        self.setWindowTitle('QPixmap 缩放显示示例')
        self.resize(800, 600)  # 设置窗口大小
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = MainWindow()

    sys.exit(app.exec())

11.4 手动调整 QPixmap 的大小

import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt6.QtGui import QPixmap
from PyQt6.QtCore import Qt


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.init()

    def init(self):
        # 加载图像
        original_pixmap = QPixmap("deng.png")

        # 手动调整图像大小
        scaled_pixmap = original_pixmap.scaled(800, 600, Qt.AspectRatioMode.KeepAspectRatio,
                                               Qt.TransformationMode.SmoothTransformation)

        # 创建一个 QLabel 并设置其内容为 QPixmap
        label = QLabel(self)
        label.setPixmap(scaled_pixmap)

        # 设置布局
        layout = QVBoxLayout()
        layout.addWidget(label)
        self.setLayout(layout)

        self.setWindowTitle('QPixmap 手动缩放显示示例')
        self.resize(800, 600)  # 设置窗口大小
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = MainWindow()

    sys.exit(app.exec())

12. 创建菜单和工具栏

PyQt6 提供了强大的菜单和工具栏支持,可以轻松添加应用程序的菜单和快捷工具。

12.1 创建菜单

从 PyQt6 的 6.0 版本开始,QAction 从 QtWidgets 模块移到了 QtGui 模块。

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6.QtGui import QAction

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('PyQt6 QAction Example')
        self.setGeometry(100, 100, 800, 600)

        # 创建菜单栏
        menubar = self.menuBar()

        # 创建文件菜单
        file_menu = menubar.addMenu('文件')

        # 添加文件菜单项
        new_action = QAction('新建', self)
        new_action.setShortcut('Ctrl+N')
        new_action.triggered.connect(self.new_file)
        file_menu.addAction(new_action)

        exit_action = QAction('退出', self)
        exit_action.setShortcut('Ctrl+Q')
        exit_action.triggered.connect(self.close)
        file_menu.addAction(exit_action)

    def new_file(self):
        print('新建文件')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

12.2 创建工具栏

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QToolBar
from PyQt6.QtGui import QIcon, QAction

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('PyQt6 ToolBar Example')
        self.setGeometry(100, 100, 800, 600)

        # 创建工具栏
        toolbar = QToolBar('Main Toolbar')
        self.addToolBar(toolbar)

        # 添加工具栏按钮
        new_action = QAction('新建', self)
        new_action.setShortcut('Ctrl+N')
        new_action.triggered.connect(self.new_file)
        toolbar.addAction(new_
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值