그래서 지루해 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 코드를 검토하고 싶을 수 있습니다. 몇 가지 징후에 따르면 동일한 변수 이름에 여러 번 값이 할당되어 문제 해결에 의미가 없습니다. 비슷하게 위젯 인스턴스의 레이아웃을'tk' 인스턴스로 제어하는 한 가지 유형의 geometry-manager 사용자가 있어야합니다. - {**'.pack()'** | **'.place()'** | **'.grid()'**} 어느 것이 든 당신의 의도에 가장 잘 어울리지 만, 위에서 보았 듯이 섞거나 연속/반복하지 마라. – user3666197