【发布时间】:2014-12-02 05:08:50
【问题描述】:
我知道您可以制作一个在单击 Tkinter 时可以执行某些操作的按钮,但是我怎样才能制作一个在单击时从一种颜色变为另一种颜色的按钮?然后,据此,我如何复制该按钮以制作它们的网格?我也会满足于一个按钮网格,这些按钮只是从一个字符变为另一个字符。
【问题讨论】:
标签: python button tkinter grid
我知道您可以制作一个在单击 Tkinter 时可以执行某些操作的按钮,但是我怎样才能制作一个在单击时从一种颜色变为另一种颜色的按钮?然后,据此,我如何复制该按钮以制作它们的网格?我也会满足于一个按钮网格,这些按钮只是从一个字符变为另一个字符。
【问题讨论】:
标签: python button tkinter grid
import Tkinter
color="red"
default_color="white"
def main(n=10):
window = Tkinter.Tk()
last_clicked = [None]
for x in range(n):
for y in range(n):
b = Tkinter.Button(window, bg=default_color, activebackground=default_color)
b.grid(column=x, row=y)
# creating the callback with "b" as the default parameter bellow "freezes" its value pointing
# to the button created in each run of the loop.
b["command"] = lambda b=b: click(b, last_clicked)
return window
def click(button, last_clicked):
if last_clicked[0]:
last_clicked[0]["bg"] = default_color
last_clicked[0]["activebackground"] = default_color
button["bg"] = color
button["activebackground"] = color
last_clicked[0] = button
w = main()
Tkinter.mainloop()
【讨论】: