所以我正在製作一個Tic-Tac-Toe程序,因爲我很無聊。我正在使用tkinter,並且我有一個程序詢問你想要的是哪一邊,然後一個窗口彈出9個按鈕。當按下其中一個按鈕時,出現一個圖像X或O。然後該按鈕被刪除。現在我正在製作程序的CPU移動部分。當按下按鈕(和刪除)時,它將會有一個循環。一旦隨機按鈕被調用,循環將被打破。我的困境是:我不知道如何檢查按鈕是否存在,或者循環應該是什麼樣子。下面是代碼:如何檢查tkinter中是否存在按鈕?
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()
@NateOlson你可能想要檢查你的代碼,有些跡象表明,有多次給同一個變量名賦值,這對於問題解決方案沒有意義。類似地,應該只有一種類型的geometry-manager用戶來控制widget實例的佈局到'tk'實例中 - 選擇{**'.pack()'** | **'.place()'** | **'.grid()'**}無論哪一個最適合你的意圖,但不要像上面看到的那樣混合或鏈接/重複一個接一個 – user3666197 2014-10-08 00:22:34