Python 如何检查tkinter中是否存在按钮?

Python 如何检查tkinter中是否存在按钮?,python,tkinter,Python,Tkinter,所以我做了一个“井字游戏”因为我很无聊。我用的是tkinter,我有一个程序,询问你想站在哪一边,然后弹出一个有9个按钮的窗口。当按下其中一个按钮时,会出现X或O的图像。然后按钮被删除。现在我正在做程序的CPU移动部分。当按下(并删除)按钮时,它将有一个循环。一旦随机按钮被调用,循环将被中断。我的困境是:我不知道如何检查按钮是否存在,或者循环应该是什么样子。代码如下: from tkinter import * import sys import time import random tk =

所以我做了一个“井字游戏”因为我很无聊。我用的是tkinter,我有一个程序,询问你想站在哪一边,然后弹出一个有9个按钮的窗口。当按下其中一个按钮时,会出现XO的图像。然后按钮被删除。现在我正在做程序的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()

与其删除按钮,不如将其隐藏(使用
.pack\u-forget()
方法),并检查按钮的可见性(.visible,我认为
.winfo\u-view()
方法)。

与其删除按钮,不如将其隐藏(使用
.pack\u-forget()
方法),并检查按钮的可见性(.visible,我认为
.winfo\u viewable()
方法)。

使用
.winfo\u exists()
方法测试小部件的存在

>>> from tkinter import *
>>> app = Tk()
>>> b = Button(text="hello")
>>> b.winfo_exists()
1
>>> b.destroy()
>>> b.winfo_exists()
0

尽量避免循环。您需要引发事件并允许事件循环尽可能多地运行。在Tk中,
.event\u generate()
方法或
.after()
调度程序方法对此很有用。

winfo\u exists()
方法测试小部件的存在

>>> from tkinter import *
>>> app = Tk()
>>> b = Button(text="hello")
>>> b.winfo_exists()
1
>>> b.destroy()
>>> b.winfo_exists()
0

尽量避免循环。您需要引发事件并允许事件循环尽可能多地运行。在Tk中,
.event\u generate()
方法或
.after()
调度程序方法对此很有用。

@NateOlson您可能需要查看代码,一些迹象显示,多次为同一变量名分配了一个值,这对问题解决方案没有意义。类似地,应该只有一种类型的几何管理器用户来控制控件实例在
tk
实例中的布局——选择{
.pack()
|
.place()
|
.grid()
}。无论哪一种最适合您的意图,但不要像看到的那样一个接一个地混合或连锁/重复above@NateOlson您可能需要查看代码,一些迹象显示,有多次为同一变量名指定了一个值,这对问题解决方案没有意义。类似地,应该只有一种类型的geometry manager用户来控制控件实例在
tk
实例中的布局——选择{
.pack()
|
.place()
|
.grid()
}。无论哪种类型最适合您的意图,但不要像上面所示那样一个接一个地混合或连锁/重复