我是Python的新手,也是Tinkter的新手。我正在试图通过一个应用程序跟踪烧坏的灯泡。我想要一个黄色方框网格,一旦点击就会变成灰色。我正在尝试调试错误:
Traceback (most recent call last):
File "C:\Python32\new2.py", line 32, in <module>
draw_rectangles()
File "C:\Python32\new2.py", line 29, in draw_rectangles
cell_zone.create_rectangle(place, 25, place+25, 50, fill=color)
File "C:\Python32\lib\tkinter\__init__.py", line 2194, in create_rectangle
return self._create('rectangle', args, kw)
File "C:\Python32\lib\tkinter\__init__.py", line 2173, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: unknown color name "42593256color"
“42593256color”每次显示不同的随机数。
from tkinter import *
master = Tk()
columns = 5
clicked = False
cell_zone = Canvas(master, width=500, height=500)
cell_zone.pack()
def color():
if clicked == True:
#color = "grey"
pass
if clicked == False:
#color = "yellow"
pass
def draw_rectangles():
for i in range(1, columns+1):
place = i * 50
print(place)
#cell_zone.create_rectangle(place, place, place*1.2, place*1.2, fill="yellow")
#cell_zone.create_rectangle(25, 25, 50, 50, fill="yellow")
#cell_zone.create_rectangle(75, 25, 100, 50, fill="green")
#cell_zone.create_rectangle(125, 25, 150, 50, fill="pink")
#cell_zone.create_rectangle(place, 25, place+25, 50, fill="yellow", command=button_color())
print(color)
cell_zone.create_rectangle(place, 25, place+25, 50, fill=color)
color()
draw_rectangles()
mainloop()
答案 0 :(得分:0)
由于color
是一个函数,也许你有意这样做:
cell-zone.create_rectangle(...fill=color())
请注意使用()
调用函数来获取颜色。但是,由于color()
实际上并没有返回颜色,因此也会失败。
您是否打算让color
持有颜色,返回颜色或其他内容?