Python Tkinter变量观测器

Python Tkinter变量观测器,python,tkinter,Python,Tkinter,我试图根据观察者的信息设置一个变量。然后这个变量,我希望在治疗中使用它,而不是显示它 #编码:utf-8 进口tkinter app=tkinter.Tk() def CountryChoice(*参数): 如果observer.get()==0: Country=“法国” 如果observer.get(): 国家=“联合王国” observer=tkinter.StringVar() 观察者跟踪(“w”,国家选择) FranceRadio=tkinter.Radiobutton(应用程序,te

我试图根据观察者的信息设置一个变量。然后这个变量,我希望在治疗中使用它,而不是显示它

#编码:utf-8
进口tkinter
app=tkinter.Tk()
def CountryChoice(*参数):
如果observer.get()==0:
Country=“法国”
如果observer.get():
国家=“联合王国”
observer=tkinter.StringVar()
观察者跟踪(“w”,国家选择)
FranceRadio=tkinter.Radiobutton(应用程序,text=“France”,值=0,变量=observer)
UKRadio=tkinter.Radiobutton(应用程序,text=“UK”,值=1,变量=observer)
FranceRadio.grid(行=0,列=0)
UKRadio.grid(行=0,列=1)
#def treatement():
#我想在这里重复使用变量Country,但我无法得到它。
打印(国家)#==>名称错误:未定义名称“国家”
app.mainloop()

目标是在用户将鼠标放在按钮上时检索
Country
变量。下一个目标是添加一个启动按钮,该按钮将根据单选按钮的选择启动流程。但我无法获取
国家变量。

首先,您将国家变量视为全局变量,但它不是。而且它在main循环之前也没有定义

def CountryChoice(*args):
    global Country
    obs = observer.get()
   if int(obs) == 0:
        Country = "France"
    if int(obs) == 1:
        Country = "United Kingdom"
这是一个开始。不过,在定义国家之前,现在不能要求打印国家。我建议您不要将“Country”声明为全局,只需从
CountryChoice
函数启动您的函数即可


有几件事妨碍了问题中的代码工作

之所以发生
name错误
,是因为执行
print(Country)
调用时,全局变量
Country
不存在。这可以通过预先定义变量(可能在脚本开头附近的某个地方)来解决

与此问题有一定关系的是,在
CountryChoice()
函数中,
Country
被视为局部变量,因此在此处设置其值不会影响同名的全局变量。这可以通过在函数开头将变量声明为
global
来解决

最后,当使用
Radiobutton
小部件时,
value
选项的类型应与所使用的tkinter变量的类型相匹配。在本例中,值是整数,因此我将
observer
的变量类型从
tk.StringVar
更改为
tk.IntVar

我做了这些更改,并在
CountryChoice()
函数的末尾添加了对
treatment()
函数的调用,以在每次调用时打印当前值

#coding:utf-8
import tkinter as tk

app = tk.Tk()

Country = None  # Declare global variable.

def CountryChoice(*args):
    global Country

    if observer.get() == 0:
        Country = "France"
    elif observer.get() == 1:
        Country = "United Kingdom"

    treatment()  # Use current value of Country.

observer = tk.IntVar()  # Use IntVar since Radiobutton values are integers.
observer.trace("w", CountryChoice)

FranceRadio = tk.Radiobutton(app, text="France", variable=observer, value=0)
UKRadio = tk.Radiobutton(app, text="UK", variable=observer, value=1)

FranceRadio.grid(row=0, column=0)
UKRadio.grid(row=0, column=1)

def treatment():
    # Reuse the variable Country.
    print(Country)

app.mainloop()
最后,我想提出一些建议。我强烈建议您阅读并开始遵循建议,特别是关于和的部分。这将使您的代码更易于操作和维护,也使其他人更易于阅读

类似地,对于这个问题的公认答案,tkinter专家提供了一些关于构造tkinter应用程序的最佳方法的极好建议,如果您遵循这些建议,就不需要使用大多数全局变量(许多人认为这是一种糟糕的编程实践)

#coding:utf-8
import tkinter as tk

app = tk.Tk()

Country = None  # Declare global variable.

def CountryChoice(*args):
    global Country

    if observer.get() == 0:
        Country = "France"
    elif observer.get() == 1:
        Country = "United Kingdom"

    treatment()  # Use current value of Country.

observer = tk.IntVar()  # Use IntVar since Radiobutton values are integers.
observer.trace("w", CountryChoice)

FranceRadio = tk.Radiobutton(app, text="France", variable=observer, value=0)
UKRadio = tk.Radiobutton(app, text="UK", variable=observer, value=1)

FranceRadio.grid(row=0, column=0)
UKRadio.grid(row=0, column=1)

def treatment():
    # Reuse the variable Country.
    print(Country)

app.mainloop()