Python tkinter radiobutton小部件,颜色不变

2024-04-28 15:09:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习Python GUI林达网由于某些原因,当导师的代码运行得非常好时,我的代码不能正常工作。我把导师的工作复制到我的代码中,但仍然不起作用。在

我的代码是:

import tkinter as tk
from tkinter import ttk
win = tk.Tk()
win.title("Python GUI")

COLOR1 = "Blue"
COLOR2 = "Gold"
COLOR3 = "Red"

# Radiobutton Callback
def radCall():
    radSel = radVar.get()
    if   radSel == 1: win.configure(background=COLOR1)
    elif radsel == 2: win.configure(background=COLOR2)
    elif radsel == 3: win.configure(background=COLOR3)

# create three Radiobuttons using one variable
radVar = tk.IntVar()
rad1 = tk.Radiobutton(win, text=COLOR1, variable=radVar, value=1, command=radCall)
rad1.grid(column=0, row=5, sticky=tk.W, columnspan=3)

rad2 = tk.Radiobutton(win, text=COLOR2, variable=radVar, value=2, command=radCall)
rad2.grid(column=1, row=5, sticky=tk.W, columnspan=3)

rad3 = tk.Radiobutton(win, text=COLOR3, variable=radVar, value=3, command=radCall)
rad3.grid(column=2, row=5, sticky=tk.W, columnspan=3)

win.mainloop()

问题是,每当我点击蓝色的单选按钮时,它就起作用了,而金色和红色在我的终端上显示错误。在

错误:

^{pr2}$

elif radsel==3时相同


Tags: 代码textconfigurevariablewintkbackgroundelif