如何在tkinter中更改Button的选项?

时间:2019-01-27 18:12:44

标签: python python-3.x button tkinter

from tkinter import *


bgColour="lightblue"

def changeColour():
    bgColour="lightgreen"

def settings():
    global window1

    window1=Tk()
    window1.minsize(width=400, height=600)
    window1.maxsize(width=400, height=600)
    window1.title("Boja pozadine")
    bg_colour1 = Frame(window1, bg=bgColour, width=400, height=600)
    bg_colour1.pack()

    naslov_bojaPozadine = Label(window1,
                                text="Background color",
                                bg=bgColour,
                                font=("Arial", 15, "bold"))
    naslov_bojaPozadine.place(x=20,y=10)

    light_blue = Button(window1, text="Light blue", bg=bgColour, bd=3)
    light_blue.place(x=20,y=50, width=100)
    light_green = Button(window1,text="light green", command=changeColour)
    light_green.place(x=20,y=80, width=100)

settings()
window1.mainloop()

如何用按钮替换所有bg颜色? 您能帮忙吗 我正在制作应用程序。 如果我有太多详细信息,为什么我需要添加更多详细信息? 这是怎么回事?

1 个答案:

答案 0 :(得分:0)

我不能完全确定我理解您的问题,但是以下内容可能会有所帮助。我向changeColour()添加了一个参数,该参数告诉您要更改的窗口小部件,并修改了传递给它的command关键字参数,以便在调用该参数时将其传递给。还向其他按钮添加了类似的command,它将颜色更改为默认颜色。

bgColour = "lightblue"  # Default background colour.

def changeColour(widget, colour):
    widget.configure(bg=colour)

def settings():
    global window1

    window1=Tk()
    window1.minsize(width=400, height=600)
    window1.maxsize(width=400, height=600)
    window1.title("Boja pozadine")
    bg_colour1 = Frame(window1, bg=bgColour, width=400, height=600)
    bg_colour1.pack()

    naslov_bojaPozadine = Label(window1,
                                text="Background color",
                                bg=bgColour,
                                font=("Arial", 15, "bold"))
    naslov_bojaPozadine.place(x=20,y=10)

    light_blue = Button(window1, text="Light blue", bg=bgColour, bd=3,
                        command=lambda: changeColour(bg_colour1, bgColour))
    light_blue.place(x=20,y=50, width=100)
    light_green = Button(window1,text="light green",
                         command=lambda: changeColour(bg_colour1, "light green"))
    light_green.place(x=20,y=80, width=100)

settings()
window1.mainloop()