2017-09-02 122 views
-1

我在lynda.com上学习Python GUI,由于某种原因,我的代码不工作,而导师的代码工作得很好。我将导师的工作复制到了我的代码中,但仍然无效。Python tkinter radiobutton部件,颜色不会改变

这里是我的代码:

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() 

的问题是,每当我点击蓝色单选按钮,它的工作原理,而金色和红色给我的错误我的终端上。

错误:

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Users\Joshua\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__ 
    return self.func(*args) 
    File "radiobuttonwidget.py", line 60, in radCall 
    elif radsel == 2: win.configure(background=COLOR2) 
NameError: name 'radsel' is not defined 

同为ELIF radsel == 3

+0

在这种情况下,“赢”是什么?你得到的错误是什么? –

+0

更新了我的问题。 win = tk.Tk() – joshua

+0

错误告诉你到底发生了什么问题。 –

回答

0

您已经定义了你的变量为radSel和在循环,您尝试使用radsel。将“s”更改为“S”。这应该是问题。