【问题标题】:Python Tkinter: Color changing grid of buttons?Python Tkinter:按钮的颜色变化网格?
【发布时间】:2014-12-02 05:08:50
【问题描述】:

我知道您可以制作一个在单击 Tkinter 时可以执行某些操作的按钮,但是我怎样才能制作一个在单击时从一种颜色变为另一种颜色的按钮?然后,据此,我如何复制该按钮以制作它们的网格?我也会满足于一个按钮网格,这些按钮只是从一个字符变为另一个字符。

【问题讨论】:

    标签: python button tkinter grid


    【解决方案1】:
    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()
    

    【讨论】:

    • 非常感谢!即使在我选择了另一个按钮后,有什么方法可以使按钮保持活动状态。我真正想要的是当我单击它们时可以在两种颜色之间更改的按钮。如果您可以提供帮助,请尝试为我指出正确的方向,而不是给我完整的答案。我还在学习,想自己尝试一些东西!
    猜你喜欢
    • 1970-01-01
    • 2018-05-17
    • 2018-09-28
    • 1970-01-01
    • 2020-04-21
    • 2018-06-10
    • 1970-01-01
    • 2017-02-22
    • 2018-10-04
    相关资源
    最近更新 更多