How to get Entry value using PY_VAR0 or PY_VAR1?

后端 未结 1 1966
醉酒成梦
醉酒成梦 2021-01-21 21:27

I have simple code like this:

from tkinter import *



def _show_value(*pargs):
    print(*pargs)



root = Tk()

entry_var1 = StringVar()
entry_var1.trace(\'w\'         


        
相关标签:
1条回答
  • 2021-01-21 21:56

    You can do it in two ways.

    First, use globalgetvar method of your root window.

    def _show_value(*pargs):
        print(*pargs)
        print(root.globalgetvar(pargs[0]))
    

    This, should give you text of respective entry.

    Second, and probably more recommended is to pass StringVar references into _show_value, using lambdas for instance, rather then using global variables of Tk:

    from tkinter import *
    
    
    
    def _show_value(v, *pargs):
        print(*pargs)
        print(v.get())
    
    
    
    root = Tk()
    
    entry_var1 = StringVar()
    entry_var1.trace('w', lambda *pargs: _show_value(entry_var1, *pargs))
    
    entry_var2 = StringVar()
    entry_var2.trace('w', lambda *pargs: _show_value(entry_var2, *pargs))
    
    
    e1 = Entry(root, textvariable=entry_var1)
    e1.pack()
    
    e2 = Entry(root, textvariable=entry_var2)
    e2.pack()      
    
    
    root.mainloop()
    
    0 讨论(0)
提交回复
热议问题