我正在嘗試在混合turtle和tkinter中制作tic tac toe專案,現在進展順利,我現在有一個錯誤。我為每個帶有坐標的欄位制作了 9 個按鈕,但是每當我單擊任何按鈕時,它都會轉到第一個寫入位置。
from tkinter import *
from turtle import *
BOARD = "board.gif"
root = Tk()
canvas = Canvas(root, width=300, height=500)
canvas.grid(padx=2, pady=2, row=0, column=0, rowspan=10, columnspan=10)
screen = TurtleScreen(canvas)
tim = RawTurtle(canvas)
draw = RawTurtle(canvas)
screen.addshape(BOARD)
img_turtle = RawTurtle(screen)
img_turtle.shape(BOARD)
def ticturtle():
tim.color("black")
tim.speed("fastest")
tim.hideturtle()
tim.pendown()
tim.begin_fill()
tim.left(315)
for i in range(2):
tim.forward(13)
tim.left(90)
tim.forward(26)
tim.right(90)
tim.forward(26)
tim.left(90)
tim.forward(13)
tim.right(270)
tim.forward(26)
tim.left(270)
tim.forward(26)
tim.left(90)
tim.end_fill()
tim.penup()
tim.seth(0)
class Tic():
def one(self):
tim.penup()
tim.setpos(-95, 42)
ticturtle()
def two(self):
tim.penup()
tim.setpos(-25, 42)
ticturtle()
def three(self):
tim.penup()
tim.setpos(45, 42)
ticturtle()
def four(self):
tim.penup()
tim.goto(-95, -22)
ticturtle()
def five(self):
tim.penup()
tim.goto(-25, -22)
ticturtle()
def six(self):
tim.penup()
tim.goto(45, -22)
ticturtle()
def seven(self):
tim.penup()
tim.goto(-95, -85)
ticturtle()
def eight(self):
tim.penup()
tim.goto(-25, -85)
ticturtle()
def nine(self):
tim.penup()
tim.goto(45, -85)
ticturtle()
def tacturtle():
tim.pendown()
tim.hideturtle()
tim.color("red")
tim.pensize(7)
tim.speed("fastest")
tim.circle(25)
tim.shapesize(12)
tim.penup()
class Tac():
def I(self):
tim.penup()
tim.setpos(-70, 42)
tacturtle()
def II(self):
tim.penup()
tim.setpos(0, 42)
tacturtle()
def III(self):
tim.penup()
tim.setpos(70, 42)
tacturtle()
def IV(self):
tim.penup()
tim.goto(-70, -22)
tacturtle()
def V(self):
tim.penup()
tim.goto(0, -22)
tacturtle()
def VI(self):
tim.penup()
tim.goto(70, -22)
tacturtle()
def VII(self):
tim.penup()
tim.goto(-70, -85)
tacturtle()
def VIII(self):
tim.penup()
tim.goto(0, -85)
tacturtle()
def IX(self):
tim.penup()
tim.goto(70, -82)
tacturtle()
tic = Tic()
tac = Tac()
x = 0
def tic_or_tac1():
global x
if x % 2 == 0:
tic.one()
x = 1
else:
tac.I()
x = 1
def tic_or_tac2():
global x
if x % 2 == 0:
tic.two()
else:
tac.II()
x = 1
def tic_or_tac3():
global x
if x % 2 == 0:
tic.three()
else:
tac.III()
x = 1
def tic_or_tac4():
global x
if x % 2 == 0:
tic.four()
else:
tac.IV()
x = 1
def tic_or_tac5():
global x
if x % 2 == 0:
tic.five()
else:
tac.V()
x = 1
def tic_or_tac6():
global x
if x % 2 == 0:
tic.six()
else:
tac.VI()
x = 1
def tic_or_tac7():
global x
if x % 2 == 0:
tic.seven()
else:
tac.VII()
x = 1
def tic_or_tac8():
global x
if x % 2 == 0:
tic.eight()
else:
tac.VIII()
x = 1
def tic_or_tac9():
global x
if x % 2 == 0:
tic.nine()
else:
tac.IX()
x = 1
def hide_me(event):
event.widget.grid_forget()
def callback(Buttons):
Buttons.bind('<Button>', hide_me)
def callback_and_hide(button):
callback(Play_Button1)
button.place_forget()
tic_or_tac1()
def callback_and_hide2(button):
callback(Play_Button2)
button.place_forget()
tic_or_tac2()
def callback_and_hide3(button):
callback(Play_Button3)
button.place_forget()
tic_or_tac3()
def callback_and_hide4(button):
callback(Play_Button4)
button.place_forget()
tic_or_tac4()
def callback_and_hide5(button):
callback(Play_Button5)
button.place_forget()
tic_or_tac5()
def callback_and_hide6(button):
callback(Play_Button6)
button.place_forget()
tic_or_tac6()
def callback_and_hide7(button):
callback(Play_Button7)
button.place_forget()
tic_or_tac7()
def callback_and_hide8(button):
callback(Play_Button8)
button.place_forget()
tic_or_tac3()
def callback_and_hide9(button):
callback(Play_Button9)
button.place_forget()
tic_or_tac9()
Play_Button1 = Button(master = root,text='??',command=lambda: callback_and_hide(Play_Button1))
Play_Button1.config(bg="white",fg="black")
Play_Button1.place(x=54, y=157, width=61, height=61)
Play_Button2 = Button(master = root, text ="??", command=lambda: callback_and_hide(Play_Button2))
Play_Button2.config(bg="white",fg="black")
Play_Button2.place(x=124, y=157, width=61, height=61)
Play_Button3 = Button(master = root, text ="??", command=lambda: callback_and_hide(Play_Button3))
Play_Button3.config(bg="white",fg="black")
Play_Button3.place(x=194, y=157, width=61, height=61)
Play_Button4 = Button(master = root, text ="??", command=lambda: callback_and_hide(Play_Button4))
Play_Button4.config(bg="white",fg="black")
Play_Button4.place(x=54, y=227, width=61, height=61)
Play_Button5 = Button(master = root, text ="??", command=lambda: callback_and_hide(Play_Button5))
Play_Button5.config(bg="white",fg="black")
Play_Button5.place(x=124, y=227, width=61, height=61)
Play_Button6 = Button(master = root, text ="??", command=lambda: callback_and_hide(Play_Button6))
Play_Button6.config(bg="white",fg="black")
Play_Button6.place(x=194, y=227, width=61, height=61)
Play_Button7 = Button(master = root, text ="??", command=lambda: callback_and_hide(Play_Button7))
Play_Button7.config(bg="white",fg="black")
Play_Button7.place(x=54, y=293, width=61, height=61)
Play_Button8 = Button(master = root, text ="??", command=lambda: callback_and_hide(Play_Button8))
Play_Button8.config(bg="white",fg="black")
Play_Button8.place(x=124, y=293, width=61, height=61)
Play_Button9 = Button(master = root, text ="??", command=lambda: callback_and_hide(Play_Button9))
Play_Button9.config(bg="white",fg="black")
Play_Button9.place(x=194, y=293, width=61, height=61)
screen.mainloop()
??? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ????
uj5u.com熱心網友回復:
你有問題,因為所有Buttons運行相同callback_and_hide,但每個都Button應該運行不同的功能callback_and_hide2,callback_and_hide3等等。就是這樣。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/435643.html
上一篇:以英寸或磅為單位的視窗大小和位置
