使用PyQt5實現(xiàn)一個鼠標連點器
前言
本次設計的鼠標連點器主要是對QVBoxLayout、QHBoxLayout和QStackedWidget進行一個回顧復習,加深對它們的理解,提高運用的熟練度。
鼠標連點器
如以下代碼所示,設計兩個QWidget控件,分別為QWidget1和QWidget2,QWidget1中設計的是連點器基本設計窗口,可輸入鼠標的點擊速度和點擊總時長;QWidget2中設計的是連點器執(zhí)行后的窗口,其中有倒計時準備和執(zhí)行過程的窗口顯示。
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): # 創(chuàng)建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) # 倒計時和執(zhí)行窗口設計 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秒執(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('開始點擊') 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_())
效果圖
運行結果:
寫一個窗口測試點擊,點擊“開始點擊”按鈕前
點擊“開始點擊”按鈕后進入5秒倒計時
開始點擊。進入設置的3秒點擊時長,0.01秒/次的點擊速度。鼠標每點擊“點擊”按鈕一次,就會在控制臺打印被點擊。
以上就是使用PyQt5實現(xiàn)一個鼠標連點器的詳細內(nèi)容,更多關于PyQt5鼠標連點器的資料請關注腳本之家其它相關文章!
相關文章
pandas中DataFrame檢測重復值的實現(xiàn)
本文主要介紹了pandas DataFrame檢測重復值,主要包括了檢查整行整列的檢測,以及多列是否重復,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05python目標檢測yolo1?yolo2?yolo3和SSD網(wǎng)絡結構對比
這篇文章主要為大家介紹了python目標檢測yolo1?yolo2?yolo3和SSD網(wǎng)絡結構對比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05