【问题标题】:How can I change the colour of a button when clicked at runtime? [duplicate]在运行时单击时如何更改按钮的颜色? [复制]
【发布时间】:2021-01-20 05:28:26
【问题描述】:
button1=Button(root,text="A1",width=8).grid(row=0,column=0)
button2=Button(root,text="A2",width=8).grid(row=0,column=1)

label1=Label(root,text="       ",padx=20).grid(row=0,column=2)

button22=Button(root,text="A3",width=8).grid(row=0,column=3,sticky='E')
button23=Button(root,text="A4",width=8).grid(row=0,column=4,sticky='E')

我正在尝试为一个学校项目制作座位安排系统。我有一个问题:单击后如何更改按钮的颜色?单击该按钮后,我想更改已预订座位和可用座位的颜色。

【问题讨论】:

    标签: python button tkinter colors


    【解决方案1】:

    如果您只想在单击时更改按钮的颜色,则需要在该按钮小部件上使用.config() 方法。

    例如,如果一个按钮被定义为

    aButton = tk.Button(root, text='change my color').pack()
    

    然后要更改颜色(或与该小部件相关的几乎所有内容,如文本、命令或其他)调用该方法

    aButton.configure(bg='#f0f', fg='#fff') # change to your required colors, bg is background, fg is foreground.
    

    OR.config()也可以使用(这2个方法完全一样)

    aButton.config(bg='#f0f', fg='#fff')
    

    现在您如何知道按钮何时为clicked。最简单直观的方法是定义functions 并将它们连接(或bind)到按钮。现在你想怎么做完全取决于用户的喜好。有些人喜欢为所有按钮创建单独的不同功能,有些人只喜欢创建一个。

    不过,对于您的情况,除了更改颜色之外您不需要做任何其他事情,因此单个功能就足够了。 重要 在下面的示例代码中,我使用了lambda 函数,这是一种特殊类型的函数,不需要单独定义。但是,这绝不是必要的

    适合您的工作示例

    from tkinter import *  # I don't recommend using global import. better use "import tkinter as tk"
    
    root = Tk()
    
    button1=Button(root,text="A1",width=8, command=lambda: button1.config(bg='#f00'))
    button1.grid(row=0,column=0)
    
    button2=Button(root,text="A2",width=8, command=lambda: button2.config(bg='#f00'))
    button2.grid(row=0,column=1)
    
    Label(root,text=" ",padx=20).grid(row=0,column=2)
    
    button22=Button(root,text="A3",width=8, command=lambda: button22.config(bg='#f00'))
    button22.grid(row=0,column=3,sticky='E')
    
    button23=Button(root,text="A4",width=8, command=lambda: button23.config(bg='#f00'))
    button23.grid(row=0,column=4,sticky='E')
    
    root.mainloop()
    

    使用函数

    from tkinter import *  # I don't recommend using global import. better use "import tkinter as tk"
    
    
    def changeColor(btn):
        # Use your own highlight background argument here instead of bg
        btn.configure(bg='#f00')
    
    
    root = Tk()
    
    button1=Button(root,text="A1",width=8, command=lambda: changeColor(button1))
    button1.grid(row=0,column=0)
    
    button2=Button(root,text="A2",width=8, command=lambda: changeColor(button2))
    button2.grid(row=0,column=1)
    
    Label(root,text=" ",padx=20).grid(row=0,column=2)
    
    button22=Button(root,text="A3",width=8, command=lambda: changeColor(button22))
    button22.grid(row=0,column=3,sticky='E')
    
    button23=Button(root,text="A4",width=8, command=lambda: changeColor(button23))
    button23.grid(row=0,column=4,sticky='E')
    
    root.mainloop()
    

    【讨论】:

    • 非常感谢。我仍然有一个问题,例如如何才能看到我尝试使用 config 的颜色变化,但它并没有从默认颜色更改为 red 。我怎样才能证明这一点?
    • 我确实复制了它并尝试运行,但是当我点击它时它不会改变颜色
    • @PS Solanki 是的,我在 Mac 上。我目前正在使用 IDLE 3.7.4。我认为这不会有什么不同,但即使我只是复制您的代码并运行它,它仍然没有发生。
    • @shalini 很有趣.. 试试THIS 代码并告诉我它是否在clicking 按钮上改变颜色。也有任何机会您使用Tkinter(不是tkinter)(带有Python2 的那个),因为Py2 预装在Mac 设备中?
    • 如果我单击测试按钮,则不会发生任何事情。不,我正在使用 tkinter。
    猜你喜欢
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 2021-07-21
    • 2019-05-20
    • 1970-01-01
    • 2021-10-18
    • 2021-07-07
    • 1970-01-01
    相关资源
    最近更新 更多