0

ラジオ ボタンを使用して 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()
4

1 に答える 1

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また、これを行う場合、この目的でのみ使用した場合は変数は必要ありません。

于 2013-03-29T16:08:48.300 に答える