我是一名新的程序员,他在使用类和Tkinter方面相对较新。我有一个问题,我的Tkinter窗口不会弹出,也不会显示任何错误。
import Tkinter
from Tkinter import Canvas, Button
root = Tkinter.Tk()
canvas = Canvas(root, width=360, height=360, bg="white")
canvas.grid()
color = canvas.create_oval(100, 100, 160, 160, fill = "white")
def changeColor(x):
canvas.delete(color)
c = canvas.create_oval(100, 100, 160, 160)
canvas.itemconfigure(c,fill = x)
pink = Button(root, text = "This is just a button",changeColor("pink"))
pink.pack()
brown = Button(root, text = "This is brown",changeColor("brown"))
brown.pack()
root.mainloop()
我想制作一个程序,其中画布中椭圆的颜色根据按钮而变化。任何有关如何解决这个问题的建议都非常感谢!
答案 0 :(得分:1)
如果您的环境隐藏了tkinter
错误消息,我会很担心。如果您再遇到这种情况,我建议尽可能在IDE /环境之外运行您的代码。
无论如何,这里是带有错误的代码,阻止它运行固定。
grid
和pack
。command
构造函数添加Button
参数。x
),以便稍后调用。代码:
import Tkinter
from Tkinter import Canvas, Button
root = Tkinter.Tk()
canvas = Canvas(root, width=360, height=360, bg="white")
canvas.grid()
color = canvas.create_oval(100, 100, 160, 160, fill = "white")
def changeColor(x):
def f():
canvas.itemconfig(color, fill = x)
return f
pink = Button(root, text = "This is just a button", command=changeColor("pink"))
pink.grid()
brown = Button(root, text = "This is brown", command=changeColor("brown"))
brown.grid()
root.mainloop()
另请注意,正如评论中所建议的那样,您无需删除并重新创建椭圆。您可以重新配置现有的。