是否可以将按钮绑定到在画布上绘制形状的函数?
我有这部分代码:
def option(*args):
global missguess
missguess=missguess+1
if missguess==1:
w.create_oval(210,100,295,175,width=3)
if missguess==2:
w.create_line(249,175,249,285,width=3)
if missguess==3:
w.create_line(249,200,270,250,width=3)
if missguess==4:
w.create_line(249,200,228,250,width=3)
if missguess==5:
w.create_line(249,285,228,350,width=3)
if missguess==6:
w.create_line(249,285,270,350,width=3)
print("You Lose!")
此选项功能绑定到许多按钮。我一直收到错误:
AttributeError: 'Button' object has no attribute 'create_oval'
我试图在选项函数中创建函数来绘制如下形状:
def option(*args):
global missguess
missguess=missguess+1
if missguess==1:
def oval():
w.create_oval(210,100,295,175,width=3)
但是画布上没有任何内容。我不确定是否有办法做到这一点。
答案 0 :(得分:0)
是的,您可以定义该功能,然后将其名称放在'命令中。按钮的参数。 这段代码只是它如何工作的一般例子,希望这有帮助
import Tkinter
window = Tkinter.Tk()
canvas = Tkinter.Canvas(window, width=640, height=480)
canvas.pack()
def draw_circle():
canvas.create_oval(25,25,70,70)
button = Tkinter.Button(window, text="draw circle", command=draw_circle)
button.pack()
window.mainloop()