我正在尝试制作一个tic tack脚趾游戏,但我似乎无法弄清楚如何删除一个按钮,另一个。我已经尝试将函数输入到pack_forget和grid_forget中,但两者都给我错误。代码在
之下import turtle
from turtle import *
from tkinter import *
root =Tk()
turtle.screen = Screen()
i = 360
up = Turtle()
up.forward(180)
up.back(360)
up.forward(360/3)
up.right(90)
up.forward(180)
up.back(275)
up.forward(95)
up.left(90)
up.forward(360/3)
up.right(90)
up.forward(180)
up.back(275)
up.forward(275-180+90)
up.right(90)
up.forward(360/3*2)
up.back(360)
def TopLeftO(event):
topleft = True
i = 360
up.seth(180)
up.up()
up.goto(-100,20)
while i > 0:
up.down()
up.speed(0)
up.forward(0.5)
up.right(1)
i-=1
i=360
event.widget.pack_forget()
#insert function to delete 2nd button
def TopLeftX(event):
topleft = True
up.up()
up.goto(-100,20)
up.seth(0)
up.down()
up.left(45)
up.forward(30+20)
up.back(15+10)
up.right(90)
up.forward(15+20)
up.back(30+40)
event.widget.pack_forget()
#insert code to delete first button
toplefto = Button(root, text = "top left for player O");
toplefto.bind('<Button-1>', TopLeftO)
toplefto.pack()
topleftx = Button(root, text = "top left for player X")
topleftx.bind('<Button-1>', TopLeftX)
topleftx.pack()
root.mainloop()
答案 0 :(得分:1)
功能TopLeftX和按钮topleftx具有相似的名称。调用topleftx.pack_forget()
会删除按钮,调用TopLeftX.pack_forget()
会引发错误。将变量和函数重命名为不同的名称将有助于消除混淆。
在python中查看二维列表以存储按钮也可能有所帮助。
我将按钮删除移到了回调的开头附近,这样一个人就无法点击另一个。
import turtle
from turtle import *
from tkinter import *
root =Tk()
turtle.screen = Screen()
i = 360
up = Turtle()
up.forward(180)
up.back(360)
up.forward(360/3)
up.right(90)
up.forward(180)
up.back(275)
up.forward(95)
up.left(90)
up.forward(360/3)
up.right(90)
up.forward(180)
up.back(275)
up.forward(275-180+90)
up.right(90)
up.forward(360/3*2)
up.back(360)
toplefto = Button(root, text = "top left for player O")
topleftx = Button(root, text = "top left for player X")
def TopLeftO(event):
topleft = True
i = 360
event.widget.pack_forget()
#insert function to delete 2nd button
topleftx.pack_forget()
up.seth(180)
up.up()
up.goto(-100,20)
while i > 0:
up.down()
up.speed(0)
up.forward(0.5)
up.right(1)
i-=1
i=360
def TopLeftX(event):
topleft = True
event.widget.pack_forget()
#insert code to delete first button
toplefto.pack_forget()
up.up()
up.goto(-100,20)
up.seth(0)
up.down()
up.left(45)
up.forward(30+20)
up.back(15+10)
up.right(90)
up.forward(15+20)
up.back(30+40)
toplefto.bind('<Button-1>', TopLeftO)
toplefto.pack()
topleftx.bind('<Button-1>', TopLeftX)
topleftx.pack()
root.mainloop()