简体   繁体   English

从动态复选按钮中获取价值?

[英]Get Value from dynamic checkbuttons?

I'm building a GUI where i have first dynamic created checkbuttons:我正在构建一个 GUI,其中我有第一个动态创建的检查按钮:

for i in range(7,spaltendf):
    
    globals()['i_{}'.format(i)] = IntVar()
    
    globals()['Check_{}'.format(i)] = Checkbutton(root,text=df [i][zeilendf], variable = 'i_{}'.format(i)).grid(row = r, column =4)
    #print('i_{}'.format(i))
    
    r = r +1

Now the User can "check" the buttons and i want implement the answer in a database (When checked than 1, when not checked 0)现在用户可以“检查”按钮,我想在数据库中实现答案(选中时大于 1,未选中时为 0)

But when I try my code it gives me only 0 as answer even though i checked the button.但是当我尝试我的代码时,即使我选中了按钮,它也只给了我 0 作为答案。 Can somebody help me ?有人可以帮助我吗?

Here is my code:这是我的代码:

def myClick():

    for z in range(7,spaltendf):
        globals()['Eingabe_{}'.format(z)]=int(globals()['i_{}'.format(z)].get())
        print(globals()['Eingabe_{}'.format(z)])

In the third line, inside the CheckButton , there's variable = 'i_{}'.format(i) .在第三行,在CheckButton内部,有variable = 'i_{}'.format(i)
This sets variable to a string, instead try using the same logic you used when you created the IntVar :这将variable设置为字符串,而是尝试使用您在创建IntVar时使用的相同逻辑:
variable = globals()['i_{}'.format(i)] . variable = globals()['i_{}'.format(i)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM