【问题标题】:Updating the background colour in Toplevel widget更新 Toplevel 小部件中的背景颜色
【发布时间】:2013-03-20 08:40:43
【问题描述】:

我正在尝试通过单选按钮更新 TopLevel 小部件的背景颜色。 我想要的是在用户更改单选按钮时更改背景颜色。 目前,该程序会打开一个带有单选按钮的新窗口。背景颜色完全没有变化。

from tkinter import *

class Example:

    def newWindow(self):
        top = Toplevel()
        v = IntVar()
        v.set(-1)
        self.aRadioButton = Radiobutton(top, text="Blue",variable = v, value = 0)
        self.aRadioButton.grid(row=1, column=1)
        self.aRadioButton = Radiobutton(top, text="Red",variable = v, value = 1)
        self.aRadioButton.grid(row=1, column=0)

        if v == 0:
            top.configure(bg="Blue")
        elif v == 1:
            top.configure(bg="Red")


    def __init__(self, master):
        frame = Frame(master, width = 50, height = 50)
        frame.grid()

        self.aLabel = Label(frame, text = "New window bg colour").grid(row=0)

        self.aButton = Button(frame, text="To new window", command=self.newWindow)
        self.aButton.grid(row=1)

root = Tk()
app = Example(root)
root.mainloop()

【问题讨论】:

  • 必须有某种事件,当您更改单选按钮时...应该会更改颜色。 newWindow 只被调用一次。

标签: python user-interface tkinter widget


【解决方案1】:

当您更改单选按钮时,您必须使用事件。 像这样将命令方法附加到单选按钮:

self.aRadioButton = Radiobutton(top, text="Blue",variable = v, value = 0, command=lambda: top.configure(bg="Blue"))
self.aRadioButton = Radiobutton(top, text="Red",variable = v, value = 1, command=lambda: top.configure(bg="Red"))

此外,当您这样做时,如果您仅将变量 v 用于此目的,则不需要它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 2011-10-30
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多