Python radiobutton get在函数内不起作用

Python radiobutton get在函数内不起作用,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我想有一个单一的按钮窗口(与文本“点击”)。当我点击按钮时,一个新窗口应该会打开。在第二个窗口中,我希望有两个单选按钮和一个按钮(带有文本“Print”)。单击该按钮(“打印”)应打印变量my_变量的当前值,该值指示选择了哪个Radiobutton 第一个代码缺少第一个窗口,但它打印出正确的值。在第二个代码中,我添加了第一个窗口(通过打开第二个窗口的“单击”按钮),它始终打印变量my_变量的默认值。每次按下“打印”按钮时,我应该更改什么以获取当前值 我将tkinter与Python 3.7结合使

我想有一个单一的按钮窗口(与文本“点击”)。当我点击按钮时,一个新窗口应该会打开。在第二个窗口中,我希望有两个单选按钮和一个按钮(带有文本“Print”)。单击该按钮(“打印”)应打印变量my_变量的当前值,该值指示选择了哪个Radiobutton

第一个代码缺少第一个窗口,但它打印出正确的值。在第二个代码中,我添加了第一个窗口(通过打开第二个窗口的“单击”按钮),它始终打印变量my_变量的默认值。每次按下“打印”按钮时,我应该更改什么以获取当前值

我将tkinter与Python 3.7结合使用

工作代码:

import tkinter as tk

def function():
    """That function print the current value of my_variable, depending on which radiobutton was choosen."""
    value = my_variable.get()
    print(value)   

window = tk.Tk()
window.title("Title")
window.geometry('200x200')

my_variable = tk.IntVar(value=0)

rbtn1 = tk.Radiobutton(window, text='one', value=1, variable=my_variable)
rbtn1.grid(row=0, column=0)
rbtn2 = tk.Radiobutton(window, text='two', value=2, variable=my_variable)
rbtn2.grid(row=1, column=0)


button = tk.Button(window, text="Print", command=function)
button.grid(row=2, column=0)
window.mainloop()
import tkinter as tk

def on_click():
    def function():
    """That function shoud print the current value of my_variable, depending on which radiobutton was choosen. But it prints the default value instead."""
        value = my_variable.get()
        print(value)   

    window = tk.Tk()
    window.title("Title")
    window.geometry('200x200')

    my_variable = tk.IntVar(value=0)

    rbtn1 = tk.Radiobutton(window, text='one', value=1, variable=my_variable)
    rbtn1.grid(row=0, column=0)
    rbtn2 = tk.Radiobutton(window, text='two', value=2, variable=my_variable)
    rbtn2.grid(row=1, column=0)


    button = tk.Button(window, text="Print", command=function)
    button.grid(row=2, column=0)

window_main = tk.Tk()
window_main.title("Title main")
window_main.geometry('400x400')

button = tk.Button(window_main, text="Click", command=lambda: on_click())
button.grid(row=0, column=0)

window_main.mainloop()
非工作代码:

import tkinter as tk

def function():
    """That function print the current value of my_variable, depending on which radiobutton was choosen."""
    value = my_variable.get()
    print(value)   

window = tk.Tk()
window.title("Title")
window.geometry('200x200')

my_variable = tk.IntVar(value=0)

rbtn1 = tk.Radiobutton(window, text='one', value=1, variable=my_variable)
rbtn1.grid(row=0, column=0)
rbtn2 = tk.Radiobutton(window, text='two', value=2, variable=my_variable)
rbtn2.grid(row=1, column=0)


button = tk.Button(window, text="Print", command=function)
button.grid(row=2, column=0)
window.mainloop()
import tkinter as tk

def on_click():
    def function():
    """That function shoud print the current value of my_variable, depending on which radiobutton was choosen. But it prints the default value instead."""
        value = my_variable.get()
        print(value)   

    window = tk.Tk()
    window.title("Title")
    window.geometry('200x200')

    my_variable = tk.IntVar(value=0)

    rbtn1 = tk.Radiobutton(window, text='one', value=1, variable=my_variable)
    rbtn1.grid(row=0, column=0)
    rbtn2 = tk.Radiobutton(window, text='two', value=2, variable=my_variable)
    rbtn2.grid(row=1, column=0)


    button = tk.Button(window, text="Print", command=function)
    button.grid(row=2, column=0)

window_main = tk.Tk()
window_main.title("Title main")
window_main.geometry('400x400')

button = tk.Button(window_main, text="Click", command=lambda: on_click())
button.grid(row=0, column=0)

window_main.mainloop()
见:

  • 单击主窗口中的按钮时,它会在单击时调用
  • 中单击
    ,每次重新分配变量值:
    my\u variable=tk.IntVar(value=0)
这就是为什么在单击主窗口中的按钮之间,
my_变量
的值不会保留的原因

您需要在单击时将
my\u变量
保持在
外部,并初始化一次,而不是每次单击时

一个很好的方法是让单击时
接受它作为参数:
单击时定义(我的变量)
,以及
命令=lambda:单击时(我的变量)


一种快速而肮脏的方法是使用
global
;它在一次性脚本中是可以接受的,但很快就会变得丑陋且难以解释。

问题是您调用了
tk.tk()
两次。如果要创建另一个窗口,请改用

要解决问题,只需更改以下一行:

import tkinter as tk

def on_click():
    def function():
        """That function should print the current value of my_variable, depending on which radiobutton was chosen. But it prints the default value instead."""
        value = my_variable.get()
        print(value)

    window = tk.Toplevel()  # CHANGED.
    window.title("Title")
    window.geometry('200x200')

    my_variable = tk.IntVar(value=0)

    rbtn1 = tk.Radiobutton(window, text='one', value=1, variable=my_variable)
    rbtn1.grid(row=0, column=0)
    rbtn2 = tk.Radiobutton(window, text='two', value=2, variable=my_variable)
    rbtn2.grid(row=1, column=0)


    button = tk.Button(window, text="Print", command=function)
    button.grid(row=2, column=0)

window_main = tk.Tk()
window_main.title("Title main")
window_main.geometry('400x400')

button = tk.Button(window_main, text="Click", command=lambda: on_click())
button.grid(row=0, column=0)

window_main.mainloop()

我已经更改了它,但不幸的是它没有帮助。9000:问题不是在单击()时调用
之间保留
我的\u变量的值,并且没有必要使用全局变量来解决问题。莫妮卡:这很好。如果您想更好地理解为什么应该避免多次调用
Tk()
,请参阅