1

I just started to look into tkinter to develop some simple GUI applications. However, I had a hard time figuring out how the control variables are used. I simply want to retrieve the status of a checkbutton(whether it is checked) and the text of an entry, but using the control variable seems not doing the job. I know my_entry.get() can also be used to get the text, but since most of the tutorials use control variables, I believe it is worth learning to use it correctly... Really appreciate your help!

here is the sample code I had:

    import tkinter as tk

    def checkbtn_callback():
       print(btn_state.get())
       print(entry_text.get())

    top = tk.Tk()
    btn_state = tk.IntVar()
    checkbtn=tk.Checkbutton(top,text='testbutton',variable=btn_state,command=checkbtn_callback)
    checkbtn.grid()

    entry_text = tk.StringVar()
    entry = tk.Entry(top, text='text entry',textvariable=entry_text)
    entry.grid()

    tk.mainloop() 
Wins
  • 377
  • 1
  • 3
  • 9
  • when I say it doesn't work that means the value from .get() is not changing regardless of the user input. – Wins Mar 31 '14 at 02:09
  • Your code works for me on OSX with python 2.7. When I click the checkbutton it prints out the proper value for it and for the entry widget. Are you certain this exact code doesn't work for you? What platform are you running this on? Also, even though lots of tutorials use "control variables", they are usually unnecessary baggage for entry widgets. – Bryan Oakley Mar 31 '14 at 02:34
  • @BryanOakley Thanks for the info, that's interesting, I am running python 3.3 on Windows 8. I am pretty sure this does not work for me, which I really don't quite understand coz this is pretty much the same code in the tutorials. – Wins Mar 31 '14 at 16:41
  • I don't understand "pretty sure this does not work". Either the code you posted is working or it's not. There shouldn't be any gray area. – Bryan Oakley Mar 31 '14 at 17:56
  • @BryanOakley Sorry for the confusion, the control variable definitely does not update on my windows 8 machine, I just tried it again. But it does work on my Linux machine. – Wins Mar 31 '14 at 19:33

0 Answers0