简体   繁体   English

Python Tkinter如何在框架处于活动状态时更改背景颜色

[英]Python Tkinter how to change background color while frame is active

i am having a problem with the following code: 我对以下代码有疑问:

def __init__(self):
    window = Tkinter.Tk()
    window.minsize(600,600)
    window.maxsize(600,600)
    window.title("test")

    #window.iconbitmap('sample.ico')

    menu_bar = Tkinter.Menu(window)

    change_color_option = Tkinter.Menu(menu_bar, tearoff=0)
    change_color_option.add_command(label = "Red", command = self.color_change(1, window))
    change_color_option.add_command(label = "Blue", command = self.color_change(2, window))
    change_color_option.add_command(label = "Yellow", command = self.color_change(3, window))
    change_color_option.add_command(label = "Green", command = self.color_change(4, window))
    change_color_option.add_command(label = "Purple", command = self.color_change(5, window))
    change_color_option.add_command(label = "Brown", command = self.color_change(6, window))

    file_menu_item = Tkinter.Menu(menu_bar, tearoff=0)
    file_menu_item.add_command(label = "Quit", command = window.quit)

    option_menu_item = Tkinter.Menu(menu_bar, tearoff=0)
    option_menu_item.add_cascade(label = "change color", menu = change_color_option)


    menu_bar.add_cascade(label="File", menu=file_menu_item)
    menu_bar.add_cascade(label="Options", menu=option_menu_item)

    window.config(menu=menu_bar)

    window.mainloop()

def color_change(self, color, window):
    if color == 1:
        window.configure(background = "#CC0000")
    elif color == 2:
        window.configure(background = "#0000CC")
    elif color == 3:
        window.configure(background = "#FFFF00")
    elif color == 4:
        window.configure(background = "#336600")
    elif color == 5:
        window.configure(background = "#660099")
    elif color == 6:
        window.configure(background = "#663300")

however my problem is that the window will take the last color (#663300) and it wont change even if i press the menu button to change color. 但是我的问题是窗口将采用最后一种颜色(#663300),即使我按菜单按钮更改颜色也不会改变。 I have to run mainloop again which is not something i want to do every time i change the color. 我必须再次运行mainloop ,每次更改颜色时我都不想这样做。 Is there any way around it? 有什么办法解决吗?

To pass an argument with the callback command, you need to use a lambda expression. 要使用callback命令传递参数,您需要使用lambda表达式。 Otherwise, Tkinter will perform the callback when the widget is made, which is why it's heading straight to the last value, as this is the last one the process flow reaches. 否则,Tkinter将在制作小部件时执行回调,这就是为什么它会直接指向最后一个值的原因,因为这是流程流到达的最后一个值。

Format your lambda expressions like this: 格式化lambda表达式,如下所示:

    change_color_option.add_command(label = "Red", command = lambda: self.color_change(1, window))

More info: http://effbot.org/zone/tkinter-callbacks.htm 更多信息: http : //effbot.org/zone/tkinter-callbacks.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM