PyQt: 自定义 / 绘制按钮,定义有效点击区域,设置快捷键,自动重复,按钮状态,按钮模拟点击PyQt: 自定义 / 绘制按钮,定义有效点击区域,设置快捷键,自动重复,按钮状态,按钮模拟点击PyQt: 自定义 / 绘制按钮,定义有效点击区域,设置快捷键,自动重复,按钮状态,按钮模拟点击PyQt: 自定义 / 绘制按钮,定义有效点击区域,设置快捷键,自动重复,按钮状态,按钮模拟点击
  • 首页
  • 博客
  • 文件
  • 书签
  • 分析
  • 登录
Search
Generic filters

PyQt: 自定义 / 绘制按钮,定义有效点击区域,设置快捷键,自动重复,按钮状态,按钮模拟点击

Published by admin at 2022年4月1日
Categories
  • Practice
Tags
import sys
from PyQt5.Qt import *
import math


class CustomButton(QAbstractButton):
    # 重写paintEvent,绘制按钮
    def paintEvent(self, e) -> None:
        painter = QPainter(self)

        color = QColor(100, 100, 50)
        line = 2
        pen = QPen(color, line)

        painter.setPen(pen)
        # 绘制文本
        painter.drawText(25, 30, self.text())
        # 绘制图标
        self.icon().paint(painter, 5, 10, 25, 30)
        # 绘制圆
        painter.drawEllipse(2, 2, 50, 50)

    # 重写hitButton,定义有效点击区域为圆内部
    def hitButton(self, pos) -> bool:
        distance_of_click_position_to_center = math.sqrt(
            math.pow(pos.x() - self.width() / 2, 2)
            + math.pow(pos.y() - self.height() / 2, 2))
        if distance_of_click_position_to_center <= self.width() / 2:
            return True
        else:
            return False


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()
        self.show()
        self.user_action()
        self.push_button_auto_click()

    def setup_ui(self):
        self.setWindowTitle("Qt桌面应用程序")
        self.resize(300, 300)
        self.label = QLabel(self)
        self.label.setText("请点击按钮")
        self.label.move(100, 50)

        self.custom_button = CustomButton(self)
        # 设置图标
        self.custom_button.setIcon(QIcon("cursor.png"))
        self.custom_button.setText("按钮")
        # 设置快捷键
        self.custom_button.setShortcut("Alt+b")
        self.custom_button.resize(54, 54)
        self.custom_button.move(100, 100)
        # 设置按钮自动重复
        self.custom_button.setAutoRepeat(True)
        self.custom_button.setAutoRepeatDelay(1000)
        self.custom_button.setAutoRepeatInterval(500)

        for i in range(3):
            self.push_button = QPushButton(self)
            self.push_button.setObjectName(f"{i + 1}")
            self.push_button.setText(f"按钮{i + 1}")
            self.push_button.move(20 + i * 80, 180)
        self.push_button_1 = self.findChild(QPushButton, "1")
        # 设置按钮按下状态
        self.push_button_1.setDown(True)
        self.push_button_2 = self.findChild(QPushButton, "2")
        # 设置按钮可选择
        self.push_button_2.setCheckable(True)
        # 设置按钮被选择状态
        self.push_button_2.setChecked(True)
        self.push_button_3 = self.findChild(QPushButton, "3")
        # 设置按钮可用状态
        # self.push_button_3.setEnabled(False)

        for i in range(3):
            self.radio_button = QRadioButton(self)
            self.radio_button.setObjectName(f"{i + 1}")
            self.radio_button.setText(f"单选框{i + 1}")
            self.radio_button.move(20 + i * 80, 220)
        self.radio_button_3 = self.findChild(QRadioButton, "3")
        # 设置按钮无排它性
        self.radio_button.setAutoExclusive(False)

        # 设置按钮有排它性,为和上面区别,设置为不同的父对象
        self.check_box_button_frame = QWidget(self)
        for i in range(3):
            self.check_box_button = QCheckBox(self.check_box_button_frame)
            self.check_box_button.setObjectName(f"{i + 1}")
            self.check_box_button.setText(f"复选框{i + 1}")
            self.check_box_button.move(i * 80, 0)
            self.check_box_button.setAutoExclusive(True)
        self.check_box_button_3 = self.findChild(QCheckBox, "3")
        self.check_box_button_frame.resize(self.check_box_button_3.width() * 3, self.check_box_button_3.height())
        self.check_box_button_frame.move(20, 260)

    def timerEvent(self, a0) -> None:
        # 设置定时器事件,按钮自动点击
        self.push_button_auto_click()

    def push_button_auto_click(self):
        # 设置按钮模拟点击
        self.push_button_3.click()
        # self.push_button_3.animateClick(1000)

    def user_action(self):
        def custom_button_clicked():
            if self.label.text() == "请点击按钮":
                self.label.setText("按钮被点击了+1")
            else:
                text = int(self.label.text()[6:]) + 1
                self.label.setText(f"按钮被点击了+{text}")
            self.label.adjustSize()

        self.custom_button.clicked.connect(custom_button_clicked)

        def push_button_3_clicked():
            if self.push_button_3.text() == "按钮3":
                self.push_button_3.setText("+1")
            else:
                text = int(self.push_button_3.text()) + 1
                self.push_button_3.setText(f"+{text}")

        self.push_button_3.clicked.connect(push_button_3_clicked)

        self.startTimer(1000)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())

发表回复 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Categories

  • 猫
  • Python
  • MySQL
  • Django
  • Html/CSS
  • JavaScript
  • Vue
  • RegExp
  • php
  • Practice
  • Virtualization
  • Linux
  • Windows
  • Android
  • NAS
  • Software
  • Hardware
  • Network
  • Router
  • Office
  • WordPress
  • SEO
  • English
  • Games
  • Recipes
  • living
  • Memorandum
  • Essays
  • 未分类

归档

©2015-2022 Alaica Blog support@alaica.com