PyQt5 - 鼠标连点器

文章目录

  • ⭐️前言
  • ⭐️鼠标连点器

⭐️前言

本次设计的鼠标连点器主要是对QVBoxLayout、QHBoxLayout和QStackedWidget进行一个回顾复习,加深对它们的理解,提高运用的熟练度。

⭐️鼠标连点器

如以下代码所示,设计两个QWidget控件,分别为QWidget1和QWidget2,QWidget1中设计的是连点器基本设计窗口,可输入鼠标的点击速度和点击总时长;QWidget2中设计的是连点器执行后的窗口,其中有倒计时准备和执行过程的窗口显示。

from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QIcon, QColor, QFont
import sys
import threading
import time
from pynput.mouse import Button, Controller


class PushButton(QPushButton):
    def __init__(self, text):
        super().__init__()

        self.setText(text)

    # 设置阴影
    def enterEvent(self, evt):
        # 创建QGraphicsDropShadowEffect对象
        shadow = QGraphicsDropShadowEffect()
        # 设置阴影颜色
        shadow.setColor(QColor(0, 0, 0))
        # 设置阴影的偏移量
        shadow.setOffset(0, 0)
        # 设置阴影的模糊半径
        shadow.setBlurRadius(15)
        # 设置阴影应用于按钮
        self.setGraphicsEffect(shadow)

    # 取消阴影
    def leaveEvent(self, evt):
        self.setGraphicsEffect(None)


class LianDianQi(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowFlags(self.windowFlags() & ~Qt.WindowMinMaxButtonsHint)   # 取消最小最大化按钮
        self.setWindowTitle('街三仔-鼠标连点器')
        self.setWindowIcon(QIcon('./img/window.png'))
        self.resize(200, 150)

        self.mouse = Controller()
        self.MouseFlag = False
        self.active = True

        self.setup_ui()

    def setup_ui(self):
        self.stacked_widget = QStackedWidget(self)
        self.setCentralWidget(self.stacked_widget)

        # 基本设置窗口设计
        self.widget1 = QWidget()
        vbox_groupBox_MousePosition = QVBoxLayout(self.widget1)

        hbox_groupBox_MousePosition1 = QHBoxLayout()
        vbox_groupBox_MousePosition.addLayout(hbox_groupBox_MousePosition1)
        hbox_groupBox_MousePosition1.addWidget(QLabel('点击速度:'))
        self.v_lineEdit = QLineEdit()
        self.v_lineEdit.setText('0.01')
        hbox_groupBox_MousePosition1.addWidget(self.v_lineEdit)
        hbox_groupBox_MousePosition1.addWidget(QLabel('秒/次'))

        hbox_groupBox_MousePosition2 = QHBoxLayout()
        vbox_groupBox_MousePosition.addLayout(hbox_groupBox_MousePosition2)
        hbox_groupBox_MousePosition2.addWidget(QLabel('点击时长:'))
        self.t_lineEdit = QLineEdit()
        self.t_lineEdit.setText('3')
        hbox_groupBox_MousePosition2.addWidget(self.t_lineEdit)
        hbox_groupBox_MousePosition2.addWidget(QLabel('秒'))

        self.PushButton_Execute_Click = PushButton('开始点击')
        self.PushButton_Execute_Click.clicked.connect(self.switch_to_widget2)

        vbox_groupBox_MousePosition.addWidget(self.PushButton_Execute_Click)

        # 倒计时和执行窗口设计
        self.widget2 = QWidget()

        vbox = QVBoxLayout(self.widget2)
        self.label1 = QLabel('倒计时')
        self.label1.setFont(QFont('黑体', 20))
        vbox.addWidget(self.label1, alignment=Qt.AlignCenter)
        self.countdown = True  # True刚进入倒计时的标志

        self.label2 = QLabel('5')
        self.label2.setFont(QFont('黑体', 40))
        vbox.addWidget(self.label2, alignment=Qt.AlignCenter)

        self.timer = QTimer()
        self.timer.timeout.connect(self.update_label)

        self.stacked_widget.addWidget(self.widget1)
        self.stacked_widget.addWidget(self.widget2)

    def switch_to_widget1(self):
        self.stacked_widget.setCurrentWidget(self.widget1)

    def switch_to_widget2(self):
        self.t_text = self.t_lineEdit.text()
        self.v_text = float(self.v_lineEdit.text())
        self.timer.start(1000)  # 1秒执行一次
        self.stacked_widget.setCurrentWidget(self.widget2)

    def update_label(self):
        text = int(self.label2.text()) - 1
        if text != -1:
            self.label2.setText(str(text))

        else:
            if self.countdown:
                self.countdown = False
                self.timer.stop()
                self.label1.setText('开始点击')
                self.label2.setText(self.t_text)
                threading.Thread(target=self.Execute_Click).start()
                self.timer.start(1000)

            else:
                self.timer.stop()
                self.label1.setText('倒计时')
                self.label2.setText('5')
                self.countdown = True
                self.switch_to_widget1()

    def Execute_Click(self):
        end_time = time.time() + float(self.t_lineEdit.text())
        while time.time() < end_time:
            self.mouse.click(Button.left)
            time.sleep(self.v_text)

    # 窗口移动
    def mousePressEvent(self, evt):
        if evt.button() == Qt.LeftButton:
            self.MouseFlag = True
            self.x_origin, self.y_origin = self.x(), self.y()
            self.x_move, self.y_move = evt.globalPos().x(), evt.globalPos().y()

    def mouseMoveEvent(self, evt):
        if self.MouseFlag == True:
            self.move(self.x_origin + (evt.globalPos().x() - self.x_move), self.y_origin + (evt.globalPos().y() - self.y_move))

    def mouseReleaseEvent(self, evt):
        self.MouseFlag = False


if __name__ == '__main__':
    app = QApplication(sys.argv)
    liandianqi = LianDianQi()
    liandianqi.show()
    sys.exit(app.exec_())

运行结果:

  • 写一个窗口测试点击,点击“开始点击”按钮前
    PyQt5 - 鼠标连点器_第1张图片
  • 点击“开始点击”按钮后进入5秒倒计时
    PyQt5 - 鼠标连点器_第2张图片
  • 开始点击。进入设置的3秒点击时长,0.01秒/次的点击速度。鼠标每点击“点击”按钮一次,就会在控制台打印被点击。
    PyQt5 - 鼠标连点器_第3张图片

你可能感兴趣的:(小玩意儿,qt,python)