【问题标题】:Python Tkinter Checkbutton IssuePython Tkinter Checkbutton问题
【发布时间】:2023-04-07 12:11:01
【问题描述】:

我有两个非常简单的复选按钮,我将它们的变量保存在一个列表 (varss) 中,还有一个按钮。但我不明白为什么当我调用 var_states (通过按下按钮)时,我无法获得 off/on 值。任何帮助表示赞赏。谢谢!

from Tkinter import *

def run():
    # create the root and the canvas
    root = Tk()
    canvas = Canvas(root, width=300, height=200)
    canvas.pack()
    class Struct: pass
    canvas.data = Struct()
    # canvas.data.givenIngredients = set()

    prefInput(canvas)
    # and launch the app
    root.mainloop()

def var_states(varss):
    for var in varss:
        var.get()

def prefInput(canvas):
    master = Tk()
    var1, var2 = IntVar(), IntVar()
    Checkbutton(master, text="male", variable=var1).grid(row=1, sticky=W)
    varss = [var1, var2]
    Checkbutton(master, text="female", variable=var2).grid(row=2, sticky=W)
    Button(master, text='Show', command=lambda:var_states(varss)).grid(row=4, sticky=W, pady=4)

run()

【问题讨论】:

    标签:
    python
    button
    checkbox
    tkinter