天天看点

用Python设计界面实现后台按键和鼠标点击操作

写了一个在窗口后台按键和点击鼠标的界面。

目标功能:绑定窗口后,实现后台的按键和鼠标点击操作,并输出操作记录。

用Python设计界面实现后台按键和鼠标点击操作

UI界面转换为python文件后的代码:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(340, 600)
        MainWindow.setMinimumSize(QtCore.QSize(340, 600))
        MainWindow.setMaximumSize(QtCore.QSize(480, 780))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(40, 82, 95, 35))
        font = QtGui.QFont()
        font.setFamily("Adobe 黑体 Std R")
        font.setPointSize(16)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(40, 132, 95, 35))
        font = QtGui.QFont()
        font.setFamily("Adobe 黑体 Std R")
        font.setPointSize(16)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(40, 316, 125, 35))
        font = QtGui.QFont()
        font.setFamily("Adobe 黑体 Std R")
        font.setPointSize(16)
        self.pushButton_3.setFont(font)
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(40, 380, 125, 35))
        font = QtGui.QFont()
        font.setFamily("Adobe 黑体 Std R")
        font.setPointSize(16)
        self.pushButton_4.setFont(font)
        self.pushButton_4.setObjectName("pushButton_4")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(152, 82, 143, 35))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(16)
        self.lineEdit.setFont(font)
        self.lineEdit.setText("")
        self.lineEdit.setObjectName("lineEdit")
        self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_5.setGeometry(QtCore.QRect(185, 380, 110, 35))
        font = QtGui.QFont()
        font.setFamily("Adobe 黑体 Std R")
        font.setPointSize(16)
        self.pushButton_5.setFont(font)
        self.pushButton_5.setObjectName("pushButton_5")
        self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_6.setGeometry(QtCore.QRect(40, 208, 125, 35))
        font = QtGui.QFont()
        font.setFamily("Adobe 黑体 Std R")
        font.setPointSize(16)
        self.pushButton_6.setFont(font)
        self.pushButton_6.setObjectName("pushButton_6")
        self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_7.setGeometry(QtCore.QRect(40, 262, 125, 35))
        font = QtGui.QFont()
        font.setFamily("Adobe 黑体 Std R")
        font.setPointSize(16)
        self.pushButton_7.setFont(font)
        self.pushButton_7.setObjectName("pushButton_7")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(185, 208, 110, 35))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(16)
        self.lineEdit_2.setFont(font)
        self.lineEdit_2.setText("")
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_8.setGeometry(QtCore.QRect(40, 32, 95, 35))
        font = QtGui.QFont()
        font.setFamily("Adobe 黑体 Std R")
        font.setPointSize(16)
        self.pushButton_8.setFont(font)
        self.pushButton_8.setObjectName("pushButton_8")
        self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_3.setGeometry(QtCore.QRect(185, 262, 110, 35))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(16)
        self.lineEdit_3.setFont(font)
        self.lineEdit_3.setText("")
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_4.setGeometry(QtCore.QRect(185, 316, 110, 35))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(16)
        self.lineEdit_4.setFont(font)
        self.lineEdit_4.setText("")
        self.lineEdit_4.setObjectName("lineEdit_4")
        self.line = QtWidgets.QFrame(self.centralwidget)
        self.line.setGeometry(QtCore.QRect(42, 178, 261, 16))
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(40, 436, 257, 91))
        self.textEdit.setObjectName("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 340, 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", "绑定后台"))
        self.pushButton_2.setText(_translate("MainWindow", "解绑窗口"))
        self.pushButton_3.setText(_translate("MainWindow", "按键"))
        self.pushButton_4.setText(_translate("MainWindow", "鼠标点击"))
        self.pushButton_5.setText(_translate("MainWindow", "鼠标右击"))
        self.pushButton_6.setText(_translate("MainWindow", "循环次数"))
        self.pushButton_7.setText(_translate("MainWindow", "间隔时间(S)"))
        self.pushButton_8.setText(_translate("MainWindow", "初始化"))
           

转换命令:

转换格式:
pyuic5 -o py文件 ui文件
--------------------------------------------------------------------
pyuic5 -o py文件的路径和文件名 ui文件的路径和文件名
--------------------------------------------------------------------
文件名称不能有空格
--------------------------------------------------------------------           

在cmd界面输入:

pyuic5 -o B:\我的游戏\天涯明月刀\天刀脚本\天刀后台键鼠界面.py B:\我的游戏\天涯明月刀\天刀脚本\天刀后台键鼠.ui           
用Python设计界面实现后台按键和鼠标点击操作

接下来,就是让这个界面能够工作。

大致流程是这样的:

先导入必要的模块。

import sys
# 使用插件必要
import win32com.client
# 导入界面文件
from 天刀后台键鼠界面 import *           

继承UI界面,这里需要继承窗体,调用父类的初始化,并且初始化自行定义的函数。

# 继承界面的类
class MyMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    """ 继承窗体:QMainWindow,和转化的py文件:Ui_MainWindow """
    def __init__(self):  # 子类的初始化函数,此时会覆盖父类Ui_MainWindow类的初始化函数
        # 调用父类的初始化接口
        """ 在子类进行初始化时,也想继承父类的初始化__init__()就通过super()实现 """
        # super(MyMainWindow,self)也可以写成super()
        super(MyMainWindow, self).__init__()  # 继承父类的__init__()
        self.setupUi(self)  # 调用父类的setupUi()函数

        # 载入自定义设置
        self.test()

    # 自定义设置
    def test(self):
        pass           

最后是程序入口,这里就可以运行设计好的界面了,并且实现其功能。

# 程序入口,程序从此处启动PYQT设计的窗体
if __name__ == '__main__':
    # 对于GUI程序必须至少有一个这样的实例来让程序运行。
    app = QtWidgets.QApplication(sys.argv)
    # 调用继承的窗体对象
    myWin = MyMainWindow()
    # 显示窗体
    myWin.show()
    # 程序关闭时退出进程
    # 有时候退出程序后,sys.exit(app.exec_())会报错,改用app.exec_()就没事
    sys.exit(app.exec_())           

下面编写具体的函数:

# 注册大漠插件
    def chushihua(self):
        self.dm = win32com.client.Dispatch('dm.dmsoft')
        # 收费注册
        dm_vip = self.dm.Reg("xf30557fc317f617eead33dfc8de3bdd4ab9043", "xfrq8e66l1c0700")  # (注册码,附加码)
        if dm_vip != 1:
            self.textEdit.setText(f'收费注册失败!返回值是{dm_vip}。')
        else:
            self.textEdit.setText(f'收费注册成功!返回值是{dm_vip}。')

    def bangding(self):
        # 获取文本
        hwnd = self.lineEdit.text()
        # 延迟
        time.sleep(0.2)
        # 激活指定窗口
        self.dm.SetWindowState(hwnd, 1)
        # 延迟
        time.sleep(0.5)
        # 检测窗口是否已经被绑定
        dm_ret = self.dm.IsBind(hwnd)
        if dm_ret == 0:  #没有绑定
            # 后台绑定窗口
            try:
                # BindWindow(hwnd,display,mouse,keypad,mode)
                dm_ret = self.dm.BindWindow(hwnd, "dx", "dx2", "dx", 101)
                self.textEdit.setText('成功绑定窗口!')
            except:
                self.textEdit.setText('绑定失败!出现错误!')
        elif dm_ret == 1:
            self.textEdit.setText('窗口1已绑定,跳过。')

    def jiebang(self):
        hwnd = self.lineEdit.text()
        # 激活指定窗口
        self.dm.SetWindowState(hwnd, 1)
        bangding = self.dm.UnBindWindow()
        if bangding == 1:
            self.textEdit.setText('窗口解绑成功')
        else:
            self.dm.ForceUnBindWindow(hwnd)
            self.textEdit.setText('解绑失败,进行强制解绑!')

    def anjian(self):
        count = self.lineEdit_2.text()
        count = int(count)
        second = self.lineEdit_3.text()
        second = int(second)
        key = self.lineEdit_4.text()
        self.textEdit.setText(f'按键{key}{count}次,每次间隔{second}秒')
        for i in range(count):
            self.dm.KeyPressChar(key)
            time.sleep(second)

    def zuoji(self):
        count = self.lineEdit_2.text()
        count = int(count)
        second = self.lineEdit_3.text()
        second = int(second)
        key = self.lineEdit_4.text()
        self.textEdit.setText(f'左键单击{count}次,每次间隔{second}秒')
        for i in range(count):
            self.dm.LeftClick()
            time.sleep(second)

    def youji(self):
        count = self.lineEdit_2.text()
        count = int(count)
        second = self.lineEdit_3.text()
        second = int(second)
        key = self.lineEdit_4.text()
        self.textEdit.setText(f'右键单击{count}次,每次间隔{second}秒')
        for i in range(count):
            self.dm.RightClick()
            time.sleep(second)           

把函数和pushButton按钮绑定起来,单击pushButton按钮则触发对应的函数。

在类MyMainWindow下的初始化函数__init__(self)下面添加:

# 单击pushButton按钮触发计算函数
    self.pushButton_8.clicked.connect(self.chushihua)
    self.pushButton.clicked.connect(self.bangding)
    self.pushButton_2.clicked.connect(self.jiebang)
    self.pushButton_3.clicked.connect(self.anjian)
    self.pushButton_4.clicked.connect(self.zuoji)
    self.pushButton_5.clicked.connect(self.youji)