如何检查 tkinter 中是否存在按钮? [英] How do I check if a button exists in tkinter?

查看:44
本文介绍了如何检查 tkinter 中是否存在按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在制作井字游戏,因为我很无聊.我正在使用 tkinter,我有一个程序会询问您想成为哪一边,然后会弹出一个带有 9 个按钮的窗口.当按下其中一个按钮时,会出现 XO 的图像.然后按钮被删除.现在我正在制作程序的 CPU 移动部分.当一个按钮被按下(并删除)时,它将有一个循环进入.一旦调用随机按钮,循环将被打破.我的困境是:我不知道如何检查按钮是否存在,或者循环应该是什么样子.代码如下:

So I'm making a Tic-Tac-Toe program because I'm bored. I am using tkinter, and I have a program that asks which side you want to be, then a window pops up with 9 buttons. When one of these buttons is pressed, an image appears of an X or O. Then the button is deleted. Right now I am making the CPU move section of the program. It will have a loop that is entered when a button is pressed (and deleted). The loop will be broken out of once a random button is invoked. My dilemma is this: I don't know how to check if a button exists, or what the loop should look like. Here is the code:

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_forget() 方法),并且检查按钮的可见性(.visible,我认为是 .winfo_viewable() 方法).

Instead of deleting the button, just hide it ( with .pack_forget() method ), and check the visibility of the button ( .visible, I think .winfo_viewable() method ).

这篇关于如何检查 tkinter 中是否存在按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆