美文网首页
pyautogui+pyqt5重复按键工具

pyautogui+pyqt5重复按键工具

作者: lissettecarlr | 来源:发表于2020-08-26 15:29 被阅读0次

    简述

    该工具就是为了按照固定频率重复键入一些字符而弄出来的,我是有这方面的需求,对观者的用处就自行寻找了,下面是软件的截图,通用PyInstaller导出成一个exe的包直接使用即可


    image.png

    第一个lineEdit输入想要无线循环输入的字符
    第二个lineEdit的时间表示你输入的字符串中每个字符键入的时间差
    第三个lineEdit的时间表示每次输入完整文本后的间隔时间
    填写完成后点击启动则已经开始无限循环,切入到你需要键入的界面即可,该方式是模拟键盘,所以也就是对最顶层的界面键入。

    实现方式

    qt designer绘制界面

    image.png

    2个push button
    3个line Edit
    3个label

    使用pyqt5转换为python文件

    pyuic5 -o loopkey_ui.py loop_key.ui
    

    生成python中最后不做修改,不然之后再通过QT desinger修改UI的时候就得重写里面的代码了,使用继承的方式用一个新的文件来对界面逻辑和控件进行修改,下列是自动生成的界面代码

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'loop_key.ui'
    #
    # Created by: PyQt5 UI code generator 5.11.3
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(644, 357)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.pushButton = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton.setGeometry(QtCore.QRect(70, 210, 191, 101))
            self.pushButton.setObjectName("pushButton")
            self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton_2.setGeometry(QtCore.QRect(370, 210, 201, 101))
            self.pushButton_2.setObjectName("pushButton_2")
            self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
            self.lineEdit.setGeometry(QtCore.QRect(250, 10, 381, 41))
            self.lineEdit.setText("")
            self.lineEdit.setObjectName("lineEdit")
            self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
            self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 242, 181))
            self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
            self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
            self.verticalLayout.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout.setObjectName("verticalLayout")
            self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
            self.label.setObjectName("label")
            self.verticalLayout.addWidget(self.label)
            self.label_3 = QtWidgets.QLabel(self.verticalLayoutWidget)
            self.label_3.setObjectName("label_3")
            self.verticalLayout.addWidget(self.label_3)
            self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget)
            self.label_2.setObjectName("label_2")
            self.verticalLayout.addWidget(self.label_2)
            self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
            self.lineEdit_2.setGeometry(QtCore.QRect(250, 80, 113, 41))
            self.lineEdit_2.setObjectName("lineEdit_2")
            self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
            self.lineEdit_3.setGeometry(QtCore.QRect(250, 150, 113, 41))
            self.lineEdit_3.setObjectName("lineEdit_3")
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 644, 23))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.pushButton.setText(_translate("MainWindow", "start"))
            self.pushButton_2.setText(_translate("MainWindow", "stop"))
            self.label.setText(_translate("MainWindow", "重复输入文本"))
            self.label_3.setText(_translate("MainWindow", "文本字符间隔时间(秒)"))
            self.label_2.setText(_translate("MainWindow", "重复间隔时间(秒)"))
    
    

    键入逻辑

    编写一个使用pyautogui的类,循环必须用一个新线程,不然会卡主UI

    import pyautogui
    import time
    import threading
    
    
    # 使用方式
    # t1 = LoopKey() t1.start()将开启一些无线循环线程
    # set_str()修改按压字符串
    # set_time_s()修改循环时间
    # push_start()启动重复输入
    # push_stop()关闭重复输入
    
    
    class LoopKey(threading.Thread):
    
        def __init__(self):
            threading.Thread.__init__(self)
            self.flag= False
            self.meg = "test"
            self.interval = 1 
            self.key_interval = 0.1
            self.closeFlag = True
    
        def run(self):
            print('start')
            while 1:
                time.sleep(self.interval)
                if(self.flag):
                    # print(self.loopkey_str)
                    pyautogui.typewrite(self.meg, interval=self.key_interval) 
                else:
                    print('0000')
                    pass
                if(self.closeFlag == False):
                    break
            print('end')
    
        def set_str(self,str):
            self.meg = str
            print(self.meg)
    
        #设置输入每条按键的间隔
        def set_interval_s(self,t):
            if(t<0.1):
                print("输入值不可小于100毫秒")
                return
            print("每次输入间隔"+str(t))
            self.interval = t
        #设置输入按键指令中每个字符的间隔
        def set_key_interval_s(self,t):
            print("字符输入间隔"+str(t))
            self.key_interval = t
    
    
        def push_start(self):
            self.flag = True
        def push_stop(self):
            self.flag = False
        def close(self):
            self.closeFlag = False
    
    # t1 = LoopKey()
    # t1.start()
    # t1.push_start()
    

    主程序

    将以上两个文件关联起来,代码中已经有注释了

    import loopkey
    import sys
    import time
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import QMessageBox,QMainWindow
    from PyQt5.QtCore import QRegExp,QVersionNumber,QT_VERSION_STR
    from PyQt5.QtGui import QIcon,QRegExpValidator, QIntValidator, QDoubleValidator
    from loopkey_ui import Ui_MainWindow
    
    class loop_main(QtWidgets.QMainWindow,Ui_MainWindow):
        def __init__(self):
            self.lk = loopkey.LoopKey()
            super(loop_main, self).__init__()
            self.setupUi(self)
            self.setWindowTitle("重复输入工具")
            self.init()
    
        def init(self):
            # 开始按键
            self.pushButton.clicked.connect(self.pushButton_event)
            # 结束按键
            self.pushButton_2.clicked.connect(self.pushButton_event2)
    
            # 限制为数字和字符
            self.lineEdit.setPlaceholderText("只能键入数字和字符") 
            reg = QRegExp("[a-zA-Z0-9]+$")
            LE1Validator = QRegExpValidator(self)
            LE1Validator.setRegExp(reg)
            self.lineEdit.setValidator(LE1Validator)
            #设置浮点数限制 范围0~86400,小数点1位
            LE2Validator = QDoubleValidator(self)
            LE2Validator.setRange(0, 86400)
            LE2Validator.setNotation(QDoubleValidator.StandardNotation)
            LE2Validator.setDecimals(1)
            self.lineEdit_2.setValidator(LE2Validator)
            self.lineEdit_2.setPlaceholderText("最小为0.1s")
    
            self.lineEdit_3.setValidator(LE2Validator)
            self.lineEdit_3.setPlaceholderText("最小为0.1s")
    
    
            # 启动后台
            self.lk.start()
    
        # 重构窗体退出函数,在退出的时候同时也结束建立的线程
        def closeEvent(self,event):
            print("123")
            self.lk.close()
    
    
    
        # 开始按键事件
        def pushButton_event(self):
            self.pushButton.setEnabled(False)
            inputMsg = self.lineEdit.text()
            if(inputMsg == ''):
                print("输入为空")
            else:
                # print(inputMsg)
                self.lk.set_str(inputMsg)
                pass
            try:
                inputKeyInterval = float(self.lineEdit_2.text())
                inputInterval = float(self.lineEdit_3.text())
    
                self.lk.set_interval_s(inputInterval)
                self.lk.set_key_interval_s(inputKeyInterval)
    
            except ValueError:
                print("传入参数错误")
            self.lk.push_start()
    
        # 结束事件按键
        def pushButton_event2(self):
            self.pushButton.setEnabled(True)
            self.lk.push_stop()
            print("222")
    
    
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
        win = loop_main()
        win.show()
        sys.exit(app.exec_())
    
    

    生成windows运行程序

    pyinstaller -F .\loopMain.py --noconsole
    

    在工程目录下生成的dist文件夹中找到exe

    相关文章

      网友评论

          本文标题:pyautogui+pyqt5重复按键工具

          本文链接:https://www.haomeiwen.com/subject/kfwprktx.html