【发布时间】:2014-04-07 21:30:33
【问题描述】:
我是 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()
【问题讨论】: