欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用PyQt5實(shí)現(xiàn)一個(gè)鼠標(biāo)連點(diǎn)器

 更新時(shí)間:2023年12月08日 11:32:33   作者:街?三?仔  
這篇文章主要為大家詳細(xì)介紹了如何使用PyQt5實(shí)現(xiàn)一個(gè)鼠標(biāo)連點(diǎn)器,從而對(duì)QVBoxLayout、QHBoxLayout和QStackedWidget進(jìn)行一個(gè)回顧復(fù)習(xí),需要的可以參考一下

前言

本次設(shè)計(jì)的鼠標(biāo)連點(diǎn)器主要是對(duì)QVBoxLayout、QHBoxLayout和QStackedWidget進(jìn)行一個(gè)回顧復(fù)習(xí),加深對(duì)它們的理解,提高運(yùn)用的熟練度。

鼠標(biāo)連點(diǎn)器

如以下代碼所示,設(shè)計(jì)兩個(gè)QWidget控件,分別為QWidget1和QWidget2,QWidget1中設(shè)計(jì)的是連點(diǎn)器基本設(shè)計(jì)窗口,可輸入鼠標(biāo)的點(diǎn)擊速度和點(diǎn)擊總時(shí)長(zhǎng);QWidget2中設(shè)計(jì)的是連點(diǎn)器執(zhí)行后的窗口,其中有倒計(jì)時(shí)準(zhǔn)備和執(zhí)行過(guò)程的窗口顯示。

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)

    # 設(shè)置陰影
    def enterEvent(self, evt):
        # 創(chuàng)建QGraphicsDropShadowEffect對(duì)象
        shadow = QGraphicsDropShadowEffect()
        # 設(shè)置陰影顏色
        shadow.setColor(QColor(0, 0, 0))
        # 設(shè)置陰影的偏移量
        shadow.setOffset(0, 0)
        # 設(shè)置陰影的模糊半徑
        shadow.setBlurRadius(15)
        # 設(shè)置陰影應(yīng)用于按鈕
        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('街三仔-鼠標(biāo)連點(diǎn)器')
        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)

        # 基本設(shè)置窗口設(shè)計(jì)
        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('點(diǎn)擊速度:'))
        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('點(diǎn)擊時(shí)長(zhǎng):'))
        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('開(kāi)始點(diǎn)擊')
        self.PushButton_Execute_Click.clicked.connect(self.switch_to_widget2)

        vbox_groupBox_MousePosition.addWidget(self.PushButton_Execute_Click)

        # 倒計(jì)時(shí)和執(zhí)行窗口設(shè)計(jì)
        self.widget2 = QWidget()

        vbox = QVBoxLayout(self.widget2)
        self.label1 = QLabel('倒計(jì)時(shí)')
        self.label1.setFont(QFont('黑體', 20))
        vbox.addWidget(self.label1, alignment=Qt.AlignCenter)
        self.countdown = True  # True剛進(jìn)入倒計(jì)時(shí)的標(biāo)志

        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秒執(zhí)行一次
        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('開(kāi)始點(diǎn)擊')
                self.label2.setText(self.t_text)
                threading.Thread(target=self.Execute_Click).start()
                self.timer.start(1000)

            else:
                self.timer.stop()
                self.label1.setText('倒計(jì)時(shí)')
                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)

    # 窗口移動(dòng)
    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_())

效果圖

運(yùn)行結(jié)果:

寫(xiě)一個(gè)窗口測(cè)試點(diǎn)擊,點(diǎn)擊“開(kāi)始點(diǎn)擊”按鈕前

點(diǎn)擊“開(kāi)始點(diǎn)擊”按鈕后進(jìn)入5秒倒計(jì)時(shí)

開(kāi)始點(diǎn)擊。進(jìn)入設(shè)置的3秒點(diǎn)擊時(shí)長(zhǎng),0.01秒/次的點(diǎn)擊速度。鼠標(biāo)每點(diǎn)擊“點(diǎn)擊”按鈕一次,就會(huì)在控制臺(tái)打印被點(diǎn)擊。

以上就是使用PyQt5實(shí)現(xiàn)一個(gè)鼠標(biāo)連點(diǎn)器的詳細(xì)內(nèi)容,更多關(guān)于PyQt5鼠標(biāo)連點(diǎn)器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論