【发布时间】:2020-04-29 19:01:24
【问题描述】:
我目前正在开发一个类似于 tic tac toe 的项目(使用 AI) 我做了一个 GUI 让用户可以玩这个机器人 但是程序并没有等待玩家选择而是因为没有价值而崩溃
所以我搜索线程,尝试了很长时间无法弄清楚它是如何工作的 我做了一些测试(如下),我的代码类似于我需要对我的代码做的事情 但它也不起作用 有人有答案吗?
import threading
import tkinter as tk
windo = tk.Tk()
windo.title("Morpion")
windo.resizable(width=tk.FALSE, height=tk.FALSE)
class Player:
def __init__(self,name):
self.name = name
self.choice = None
def Change_Var(x):
print(P.choice)
P.choice = x
print(P.choice)
play_event.set()
def boucle():
i = 0
while not play_event.isSet() and i < 3000:
print(i)
i += 1
P = Player("Deltix")
start = tk.Button(height=2,width=8, text = "Start",command = lambda x = 0 : Change_Var(x))
start.grid(column = 2, row = 3,pady = 5)
play_event = threading.Event()
threading.Thread(target = windo.mainloop()).start
threading.Thread(target = boucle()).start```
【问题讨论】:
-
您需要完成有关响应按钮单击的教程。
-
target = boucle():阅读why-is-button-parameter-command-executed-when-declared和Tkinter understanding mainloop -
你绝对不需要线程。
-
你知道 tkinter 中的事件吗?您可以将键绑定到 tkinter 中的任何事件。不需要线程。
标签: python multithreading events tkinter