【问题标题】:Press Button, Change Label Colour按下按钮,更改标签颜色
【发布时间】:2017-04-30 11:19:46
【问题描述】:

我希望能够在按下按钮时更改标签的背景颜色。我目前有以下代码。

def violet_btn():
    bg = 'violet'

def violet_label():
    if violet_btn == True:
        color = 'violet'
    else:
        color = 'grey'
violet_l = Label(the_window, bg = 'grey', height= 1, width = 8)
violet_l.grid(row=1, column=1, padx=5, pady=3)

violet = Button(the_window, text = 'Violet', height= 1, width = 8, command = violet_label)

violet.grid(row=2, column=1, padx=5, pady=3)

我知道它不够复杂,但我是 python 新手,不知道还要添加什么

【问题讨论】:

    标签: python python-2.7 tkinter


    【解决方案1】:

    需要调用按钮的configure()方法;使用cget() 读取当前颜色,以便您可以轻松地在两种颜色之间切换:

    def violet_label():
        current = violet.cget('bg')
        new_colour = 'violet' if current == 'grey' else 'grey'
        violet.configure(bg=new_colour)
    

    【讨论】:

      猜你喜欢
      • 2019-11-24
      • 2023-04-02
      • 2014-04-16
      • 2023-02-21
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 2015-10-01
      • 1970-01-01
      相关资源
      最近更新 更多