所以我正在制作一个Tic-Tac-Toe计划因为我很无聊。我正在使用tkinter,我有一个程序询问你想要哪一面,然后一个窗口弹出9个按钮。按下其中一个按钮时,图像将显示为 X 或 O 。然后删除该按钮。现在我正在制作程序的CPU移动部分。当按下(并删除)按钮时,它将有一个循环。一旦调用随机按钮,循环就会被打破。我的困境是:我不知道如何检查按钮是否存在,或者循环应该是什么样子。这是代码:
from tkinter import *
import sys
import time
import random
tk = Tk()
xoro = input("Choose a side, X or O.")
if xoro == "x":
side = 0
elif xoro == "o":
side = 1
else:
print("Please enter x or o.")
canvas = Canvas(tk, width=498, height=498, background="green")
canvas.pack()
tk.title("Tic-Tac-Toe!")
time.sleep(2)
photo2 = PhotoImage(file="C:\Python34\X.gif")
photo1 = PhotoImage(file="C:\Python34\O.gif")
photolist = [photo2, photo1]
def PlaceImage(photo, x, y, buttontodelete):
canvas.create_image(x,y, image=photo)
buttontodelete.destroy()
button1 = Button(tk, text="Choose Me!")
button1 = Button(tk, text="Choose Me!", bg="green", command=lambda: PlaceImage(photolist[side], 75, 75, button1))
button1.pack()
button1.place(bordermode=OUTSIDE, height=166, width=166)
button1.place(x=0, y=0)
button1.config(highlightbackground="black")
button2 = Button(tk, text="Choose Me!")
button2 = Button(tk, text="Choose Me!", bg="green", command=lambda: PlaceImage(photolist[side], 75, 241, button2))
button2.pack()
button2.place(bordermode=OUTSIDE, height=166, width=166)
button2.place(x=0, y=166)
button2.config(highlightbackground="black")
button3 = Button(tk, text="Choose Me!")
button3 = Button(tk, text="Choose Me!", bg="green", command=lambda: PlaceImage(photolist[side], 75, 407, button3))
button3.pack()
button3.place(bordermode=OUTSIDE, height=166, width=166)
button3.place(x=0, y=332)
button3.config(highlightbackground="black")
button4 = Button(tk, text="Choose Me!")
button4 = Button(tk, text="Choose Me!", bg="green", command=lambda: PlaceImage(photolist[side], 241, 75, button4))
button4.pack()
button4.place(bordermode=OUTSIDE, height=166, width=166)
button4.place(x=166, y=0)
button4.config(highlightbackground="black")
button5 = Button(tk, text="Choose Me!")
button5 = Button(tk, text="Choose Me!", bg="green", command=lambda: PlaceImage(photolist[side], 241, 241, button5))
button5.pack()
button5.place(bordermode=OUTSIDE, height=166, width=166)
button5.place(x=166, y=166)
button5.config(highlightbackground="black")
button6 = Button(tk, text="Choose Me!")
button6 = Button(tk, text="Choose Me!", bg="green", command=lambda: PlaceImage(photolist[side], 241, 407, button6))
button6.pack()
button6.place(bordermode=OUTSIDE, height=166, width=166)
button6.place(x=166, y=332)
button6.config(highlightbackground="black")
button7 = Button(tk, text="Choose Me!")
button7 = Button(tk, text="Choose Me!", bg="green", command=lambda: PlaceImage(photolist[side], 407, 75, button7))
button7.pack()
button7.place(bordermode=OUTSIDE, height=166, width=166)
button7.place(x=332, y=0)
button7.config(highlightbackground="black")
button8 = Button(tk, text="Choose Me!")
button8 = Button(tk, text="Choose Me!", bg="green", command=lambda: PlaceImage(photolist[side], 407, 241, button8))
button8.pack()
button8.place(bordermode=OUTSIDE, height=166, width=166)
button8.place(x=332, y=166)
button8.config(highlightbackground="black")
button9 = Button(tk, text="Choose Me!")
button9 = Button(tk, text="Choose Me!", bg="green", command=lambda: PlaceImage(photolist[side], 407, 407, button9))
button9.pack()
button9.place(bordermode=OUTSIDE, height=166, width=166)
button9.place(x=332, y=332)
button9.config(highlightbackground="black")
buttonlist = [button1, button2, button3, button4, button5, button6, button7, button8, button9]
randombutton = random.randint(0, 8)
WHILE BLAH IS TRUE:
buttonlist[randombutton].invoke()
BREAK OUT OF LOOP, AND BEGIN USER'S TURN
mainloop()
答案 0 :(得分:2)
.winfo_exists()
方法测试小部件的存在。
>>> from tkinter import *
>>> app = Tk()
>>> b = Button(text="hello")
>>> b.winfo_exists()
1
>>> b.destroy()
>>> b.winfo_exists()
0
尽量避免循环。您需要引发事件并允许事件循环尽可能多地运行。在Tk中, .event_generate()
方法或 .after()
调度程序方法对此非常有用。
答案 1 :(得分:1)
不要删除按钮,只需隐藏它(使用 .pack_forget()
方法),然后检查按钮的可见性(.visible,我认为 .winfo_viewable()
方法)。