寫了一個在視窗背景按鍵和點選滑鼠的界面。
目标功能:綁定視窗後,實作背景的按鍵和滑鼠點選操作,并輸出操作記錄。
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
接下來,就是讓這個界面能夠工作。
大緻流程是這樣的:
先導入必要的子產品。
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)