Tkinter窗口冻结

时间:2020-05-04 15:23:34

标签: python tkinter artificial-intelligence freeze

我正在尝试制作一款击败游戏“ Whac-A-Mole”的AI(可以接管鼠标)。一切正常,但是当我将其转换为exe时,我无法停止它。我正在尝试制作一个可以根据用户的要求运行和停止AI的Tkinter应用程序。该程序可以启动AI并结束它,您只需要关闭窗口即可。除此以外,该窗口在AI运行之后冻结,并且我无法关闭Tkinter窗口,这意味着AI不会停止。这是我的代码:

from ctypes import windll
import tkinter as tk
from tkinter import ttk
from pynput.mouse import Controller
from pynput.mouse import Button, Controller
import time
import webbrowser
from tkinter import messagebox
from tkinter import *

def callback():
    if messagebox.askokcancel("Quit", "Are you sure you want to quit this VERY SMART AI?"):
        stop = 2
        print('yeah')
        window.destroy()


def start():   
    hdc = windll.user32.GetDC(0)
    mouse = Controller()
    webbrowser.open('https://www.classicgame.co.uk/game/Whack+a+Mole#')
    time.sleep(5)
    def wait_until(x, y, operation, colour, wait):
        hdc = windll.user32.GetDC(0)
        if operation == '!':
            while windll.gdi32.GetPixel(hdc, x, y) != colour:
                time.sleep(wait)
        if operation == '=':
            while windll.gdi32.GetPixel(hdc, x, y) == colour:
                time.sleep(wait)

    def click_on(x, y, wait):
        mouse.position = (x, y)
        mouse.click(Button.left, 1)
        time.sleep(wait)

    wait_until(601, 555, '!', 107614, 1)

    time.sleep(10)
    mouse.scroll(0, -13)
    time.sleep(1)

    click_on(597, 410, 0)

    wait_until(1030, 560, '=', 3156775, 2)

    time.sleep(10)
    mouse.position = (1030, 560)
    time.sleep(0.5)
    mouse.click(Button.left, 1)

    wait_until(657, 401, '!', 16777215, 1)

    click_on(652, 390, 1)

    click_on(568, 525, 1)

    click_on(687 ,460, 0)

    start = time.time()
    end = start + 60


    x_pos = [420, 570, 715, 420, 570, 715, 420, 570, 715,]
    y_pos = [310, 310, 310, 435, 435, 435, 560, 560, 560]
    colour_mole = [15263718, 10277608]

    time.sleep(1)


    while time.time() <= end or stop == 1:
        for x in x_pos:
            for y in y_pos:
                if windll.gdi32.GetPixel(hdc, x, y) in colour_mole:
                    mouse.position = (x, y)
                    mouse.click(Button.left, 1)

window = tk.Tk()
window.title("AI")
label = ttk.Label(window, text="Welcome to my Whac-A-Mole AI!")
label2 = ttk.Label(window, text="Press ESC to exit the program")
btn = ttk.Button(window, text="Start", command = start)
btn.grid(row=2, column=0)
label.grid(row=0, column=0)
label2.grid(row=1, column=0)


window.protocol("WM_DELETE_WINDOW", callback)

window.mainloop()

有人可以解释一下为什么冻结了,我该如何解决?也许有人可以告诉我另一种停止AI的方法。按下“ ESC”键时,我试图停止代码,但这没用。

0 个答案:

没有答案
相关问题