The following answers are based on ChatGPT and GISer Liu:
You can use the following steps to achieve this function:
-
Add a task to be performed in the list box of the key sprit, such as 1234, 2341, 4312, and so on.
-
In your code, use the pyautogui.locateOnScreen function to locate the "List item" icon in the list box and click on it using the pyautogui.click function to expand the list of tasks.
< / li > < li >use pyautogui. LocateAllOnScreen function orientation to the task list of all the tasks, and save them to a list.
-
According to the sequence of tasks that need to be executed, traverse the sequence and execute the corresponding subroutine in turn. You can use the importlib module to dynamically load and execute the corresponding subroutine. For example, if the task sequence is 4231, you can execute subtask_4, subtask_2, subtask_3, and subtask_1 in sequence.
Here is an example program that demonstrates how to execute the appropriate subroutine through the keystroke Sprite's list box:
import pyautogui
import importlib
# 定义子程序
def subtask_1():
print("执行子程序1")
def subtask_2():
print("执行子程序2")
def subtask_3():
print("执行子程序3")
def subtask_4():
print("执行子程序4")
# 执行任务序列
task_sequence = [4, 2, 3, 1]
# 定位任务列表框并单击列表项图标
listbox_icon = pyautogui.locateOnScreen("listbox_icon.png")
if listbox_icon is not None:
pyautogui.click(listbox_icon)
# 定位任务列表中的所有任务
tasks = []
for task_image in pyautogui.locateAllOnScreen("task_*.png"):
task_name = task_image.split("_")[1]
tasks.append(int(task_name))
# 按照任务序列依次执行子程序
for task_number in task_sequence:
task_name = "subtask_" + str(task_number)
if task_number in tasks:
module = importlib.import_module(__name__)
subtask = getattr(module, task_name)
subtask()
else:
print("未找到任务:", task_name)
In this example program, we first define four subroutines corresponding to the numbers 1, 2, 3, and 4 in the sequence of tasks. We then define a task sequence, task_sequence, where the order of the numbers represents the order of the subroutines that need to be executed. Next, we use the pyautogui.locateOnScreen function to locate the "list item" icon in the list box of the key Sprite and click on it with the pyautogui.click function to expand the list of tasks. . Then, we use pyautogui locateAllOnScreen function orientation to the task list of all the tasks, and save them to a list. Finally, we execute the corresponding subroutine in sequence of tasks. If a number in the task sequence is not in the task list, the program prints a message indicating that the corresponding task is not found.