【问题标题】:Tkinter get selected value of a CheckButtonTkinter 获取 CheckButton 的选定值
【发布时间】:2013-11-22 14:31:25
【问题描述】:

我已经尝试了几个examples from stackoverflow,但不幸的是没有为我工作。

我只想获取Tkinter,Python的选中checkButton的值。

我有一个 CheckButton 列表,如下所示

## csv file has rows like
# 101, apple
# 102, orange
for row in csvReader:
        checkButton = Checkbutton(top, text = row[1], variable = StringVar(), 
                 onvalue = row[0], offvalue = "0", height=2, \
                 width = 0, justify=Tkinter.LEFT)
        checkButton.pack()
        checkBoxList.append(checkButton)

点击表单中的按钮时,这里是需要获取复选框选中值的回调。

def btnStartCallBack():
    for chkBox in checkBoxList:
        print chkBox.variable().get()
        # also tried below
        # print chkBox.get()
        # print chkBox.var()
        # print chkBox.onvalue.get()

它返回:

AttributeError: Checkbutton instance has no attribute 'variable'

我只是想知道 CheckButton 被选中时是否可以获取它们的值。还有我应该在哪个属性上寻找这个?

【问题讨论】:

    标签: python python-2.7 tkinter


    【解决方案1】:

    我通常在课堂上完成我的 GUI,例如 http://zetcode.com/。我会做类似的事情

    self.v = StringVar()
    self.cb1 = CheckButton( self, text=row[1], variable=self.v )
    

    然后……

    self.v.get()
    

    我认为您可能需要在代码中以不同方式声明 variable。祝你好运!

    【讨论】:

    • 是的,你是对的。它也相似。我必须以不同的方式声明变量。 CheckVar1 = StringVar()Checkvar1.get() 返回值。容易...谢谢m8...
    • 没问题。我记得我第一次学习这些东西时——我不知道我必须使用get(),我花了好几个小时才弄明白。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多