This tutorial is set by the author to use the CC by-NC protocol.
The writing desk can create a mini mini game. By completing this mini -game, you can get a honeycomb and specific hybrid notes.
So is there a way to automatically play this game?The answer is yes, we can use Pyautogui in Python to do this.
Import Tkinter as tkFrom TKINTER Import FileDialog
From threading import thread
From time import space
Import Queue
Import pyautogui
import json
def Threaded_function (log_queue):
'' 'The main logical code' ''
LoopCount = 1
Delay = 0.1
Start = Tuple (int (i) for I in Bee_entry.get (). Split (",", ") if i)#IF I can remove the empty elements in the tuple
Points = []
For point in points_text.get ("1.0", tk.end) .split (""
"):
Temp = Tuple (int (i) for I in point.split (",", ") if i)
If tempmp:
Points.append (Temp)#but if i cannot remove the empty ancestor
DEF My_CLICK (Point: Tuple):
'' '
Yuanzu who passed into the point, move and click.
Pyautogui's click method plus coordinates will cause the previous steps to hold it in hand and cannot be used.'' '
Pyautogui.moveto (Point [0], Point [1])
Sleep (Delay)
Pyautogui.click ()
Sleep (Delay)
DEF WHETHER_LOSE (POINT: Tuple)-> Bool:
'' 'The turtle group of the passing point and determined whether it was "×", and the True was failed.'' '
Pyautogui.moveto (1, 1)#Mouse reset to prevent it from being blocked to "×"
Sleep (Delay)
Pix = pyautogui.pixel (Point [0], Point [1])
If pix == (255, 73, 64):
Return (true)
Else:
Return (False)
While True:
My_CLICK (Start)
My_CLICK (Start)
#Repeat twice to re -put the bee
Losebook = false
For I in Range (Len (POINTS)):
My_CLICK (POINTS [i]) IF I%2 == 1:
Lose = WHETHER_LOSE (POINTS [i])
If lose:
Losebook = true
Break
If losebook:
LOG_QUEUE.PUT (f "No. {loopCount} times fails.
")
Else:
LOG_QUEUE.PUT (f "No. {LoopCount} times successfully operates successfully.
")
LoopCount+= 1
DEF SAVE ():
Savelist = [bee_entry.get (), points_text.get ("1.0", tk.end)]
File = filedialog.asksaveasfilename (
DefaultExtent = ". Json",
Filetypes = [("text files", "*.json"), ("all files", "*.*"),
Title = "Save AS"
) With Open (File, "W", Encoding = 'UTF-8') AS F:
Json.dump (Savelist, F, Ensure_ascii = False, Indent = 4)
def load ():
File_path = FileDialog.askopenFilename (
DefaultExtent = ". Json",
Filetypes = [("text files", "*.json"), ("all files", "*.*"),
Title = "Open File"
Cure
If file_path:
#Use the Open function to open the file and specify the encoding method as UTF-8
With open (file_path, "r", enCoding = "UTF-8") as file:
Loadlist = json.load (file)
Bee_entry.delete (0, tk.end)
Bee_entry.insert (tk.end, loadlist [0])
Points_text.delete (1.0, tk.end)
Points_text.insert (tk.end, loadlist [1])
DEF UPDATE_LOG (log_queue):
Try:
While True:#Try to read all the messages in the queue
Log_message = log_queue.get_nowait ()#l
Log_text.see (tk.end)#roll to the bottom of the text box
Except queue.empty:
Pass#If the queue is empty, do nothing
ROOT.AFTER (300, Lambda: Update_log (log_queue))#r r r r ourselves every 300ms to check the new message
DEF Start_thread (log_queue):
Log_text.delete (1.0, tk.end)
T = Thread (target = threaded_function, args = (log_queue,))
T.DAEMON = TRUE#Set as a guardian thread to ensure that it will end when the main program is withdrawn
T.start ()
Update_log (log_queue)#Start updating the loop of the log
root = tk.tk ()
root.tital ("Forestry Writing Design Automation")
root.geometry ("600x400")
root.attributes ('-Topmost', 1)#window top
root.Resizable (width = false, height = false)
DEF FONT (size):
Return (('Microsoft Yahei', SIZE))
log_text = tk.text (root, font = font (10))
log_text.place (anchor = "center", x = 450, y = 200, width = 300, height = 400)
LOG_Text.Insert (tk.end, "This script is used for automation of forestry writing desk. Please run by administrator.
The window on the right is a log.
"++
"Fill in the coordinates of the bee placed point above the left (example: 700,445)
Fill in the coordinates of the point you want to click on the left."++
"
Note: The coordinates only need to give the form of X comma y.The coordinates of the point of the point of clicking are red in FF3333 (255, 73, 64).
"+" On the left is used to read the coordinates saved before, and the right side is loaded the previous coordinates.If you do n’t stop the code, you can move the mouse to the upper right corner if you stop. Pyautogui will automatically stop in an error when the screen boundary.
"++
"Put the bees in the writing desk first, then fill in all the necessary information before starting.")
entry_frame = tk.frame (root)
Entry_frame.Place (width = 300, height = 50)
bee_lable = tk.label (entry_frame, text = "bee placement point", font = font (13))
bee_lable.grid (row = 0, colorn = 0)
bee_entry = tk.entry (entry_frame)
bee_entry.grid (row = 0, column = 1)
points_text = tk.text (root)
Points_text.place (anchor = "center", x = 150, y = 225, width = 300, height = 350)
load_button = tk.button (root, text = "read", command = load)
load_button.place (anchor = "center", x = 50, y = 350, width = 100, height = 100)
log_queue = queue.queue ()#Create a queue for thread communication
start_button = tk.button (root, text = "Start", Command = Lambda: Start_thread (log_queue))
start_button.place (anchor = "center", x = 150, y = 350, width = 100, height = 100)
save_button = tk.button (root, text = "Save", Command = Save)
Save_button.place (anchor = "center", x = 250, y = 350, width = 100, height = 100)
root.mainloop ()