Tkinter Checkbutton不会更改变量

问题描述

复选框tkinter python有什么问题。无论我是否勾选复选框,我总是从变量中得到0。我也尝试为变量设置值,但是checkbutton并没有改变!我怎么了?

from tkinter import *
master_1 = Tk()


def show_hide_option_func():
    master = Tk()
    Label(master,text="Your sex:").grid(row=0,sticky=W)
    check_list = Intvar()
    check_song = Intvar()
    check_vol = Intvar()

    def var_state():
        print(check_list.get())
        print(check_song.get())
        print(check_vol.get())

    b= Checkbutton(master,text="female",variable=check_list).grid(row=2,sticky=W)
    c= Checkbutton(master,text="male",variable=check_song).grid(row=3,sticky=W)
    d= Checkbutton(master,text="feme",variable=check_vol).grid(row=4,sticky=W)

    Button(master,text='Quit',command=master.quit).grid(row=5,sticky=W,pady=4)
    Button(master,text='Show',command=var_state).grid(row=6,pady=4)
    master.mainloop()

Button(master_1,command=show_hide_option_func,text='Click here').pack()
master_1.mainloop()

enter image description here

谢谢。

解决方法

您不应创建多个根窗口,请参见Why are multiple instances of Tk discouraged?

使用Toplevel()创建对话框:

def show_hide_option_func():
    master = Toplevel()
    etc...