Python Tkinter:按钮的颜色更改网格? [英] Python Tkinter: Color changing grid of buttons?
本文介绍了Python Tkinter:按钮的颜色更改网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道您可以创建一个按钮,用Tkinter单击该按钮可以执行某些操作,但是我如何才能制作一个在单击时从一种颜色变为另一种颜色的按钮呢?然后,如何从中复制该按钮以使其网格化?我还会选择从一个字符更改为另一个字符的按钮网格.
I understand that you can make a button that can do some actions when clicked with Tkinter, but how can I just make a button that turns from one color to another when clicked? Then, from that, how do I replicate that button to make a grid of them? I would also settle for a grid of buttons that just change from one character to another.
推荐答案
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()
这篇关于Python Tkinter:按钮的颜色更改网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文