如何更新Tkinter GUI的一个元素?

时间:2015-12-31 23:05:49

标签: python python-2.7 tkinter

我有2个函数,MakeGUI:

def makeGUI():

    #Define the global variables of the function.
    global root,f1,f2,f3,r,y,b,g,patColor

    #Initialize Gui Items.
    root = Tk()
    root.wm_title("Buttons")
    f3 = Frame(root)
    f1 = Frame(root)
    f2 = Frame(root)
    r = Button(f1, text= "Red", fg="Black", bg="Red", width=25, font="TimesNewRoman", bd=1)
    y = Button(f1, text= "Yellow", fg="Black", bg="Yellow", width=25, font="TimesNewRoman", bd=1)
    b = Button(f2, text= "Blue", fg="Black", bg="Blue", width=25, font="TimesNewRoman", bd=1, command=showPattern)
    g = Button(f2, text= "Green", fg="Black", bg="Green", width=25, font="TimesNewRoman", bd=1)
    patColor = Label(f3, bg="White", width=66)

    #Pack the GUI items so they will show.
    f3.pack()
    f1.pack()
    f2.pack()
    r.pack(side=LEFT)
    y.pack(side=RIGHT)
    b.pack(side=LEFT)
    g.pack(side=RIGHT)
    patColor.pack()

    #Show the GUI.
    root.mainloop()

showPattern:

def showPattern():
    patColor.bg = "Blue"

有没有办法只更新patColor bg属性而不刷新整个GUI?我正在制作一个simon用python 2.7进行类型游戏,我需要它来循环一个模式数组。

1 个答案:

答案 0 :(得分:2)

我在仔细研究了正确的符号之后发现了:

patColor["bg"] = "Color"