1

I'm a beginner trying to code a program that will add or delete a widget (checkbutton) depending on the value of the entry. For example, if the entry is greater than 1, the checkbox will be added, otherwise, the program will delete the checkbox.

My code will create the widget but won't delete it after changing the entry and I can't figure it out. Can you help me please?

Thank you in advance for your help!

Here's a short sample of my code:

from Tkinter import *

root = Tk()

def update(*args):
    print EntryVar.get()

    if EntryVar.get()>'1':
        cb = Checkbutton(root,text="Show check box")
        cb.grid(row = 1)

    elif EntryVar.get()<='1' or EntryVar.get() == '':
        try:
            cb.grid_remove()
        except:
            pass

EntryVar = StringVar()
EntryVar.trace('w',update)
Entry = Entry(root,textvariable = EntryVar)
Entry.grid(row = 0)


root.mainloop()

Let me know if you need more details :)

  • so, when you type anything in your Entry box, the checkbox appears, but when you delete everything in it, you want the checkbox widget to be removed? – glls May 25 '16 at 23:24
  • Yes anything less than 1 (<=1) or if I have a blank entry box! @glls – Elie khalifeh May 25 '16 at 23:27
  • When I type a number greater than 1, I want the checkbox to appear and if I type a number less than 1 or if I leave the entry box empty I want that checkbox to be deleted. @glls – Elie khalifeh May 25 '16 at 23:28
  • as is, it creates the widget with any string entered – glls May 25 '16 at 23:30
  • Yes but it won't delete the checkbox if I then type a number less than 1 or if I delete everything in the entry box, that's my problem! Do you know what might be causing this problem? @glls – Elie khalifeh May 25 '16 at 23:32

1 Answers1

0

One problem is that cb is "unresolved" under try:, once you declare it global, it will kind of "behave" as you want:

from Tkinter import *

root = Tk()

def update(*args):
    print EntryVar.get()
    global cb
    if EntryVar.get()>'1':
        cb = Checkbutton(root,text="Show check box")
        cb.grid(row = 1)

    elif EntryVar.get()<='1' or EntryVar.get() == '':
        try:
            cb.grid_remove()

        except:
            pass

EntryVar = StringVar()
EntryVar.trace('w',update)
Entry = Entry(root,textvariable = EntryVar)
Entry.grid(row = 0)


root.mainloop()

but even there, you will face another setback when fetching the Entry value since Strings are compared lexicographically, and dissimilar types are compared by the name of their type, this issue had been raised here

Another point, if you remvove your exception handling, you will see that if a user types 0 or 1 your program will crash with "NameError: name 'cb' is not defined" because your checkbutton doesnt exist (not defined) and there is nothing to delete, IF you define the checkbutton at the beggining of your function, then it will not be deleted when you try grid_remove() or delete(). Also, when you enter strings that are non numerical, your program will not have the desired behavior. Consider changing the logic and var types you are using for this. Also note that when if EntryVar.get() > '2': (you can play around with this) behavior changes, so there is definitely something fishy with the logic.

Community
  • 1
  • 1
glls
  • 2,325
  • 1
  • 22
  • 39
  • This partially works!! There's still a problem: if the entry is greater than 20 it won't work anymore. Let's say I entered 20 in the entry box, the checkbox will appear, but if I change the entry to 1 or zero (or leave the entry box blank), the checkbox won't disappear! Do you know why this is happening? Thanks again for your help, it's really appreciated! :) – Elie khalifeh May 25 '16 at 23:38
  • Thanks for your help! :) @glls – Elie khalifeh May 26 '16 at 01:01
  • i cant figure out why when the string is > '20' the checkbox wont be removed. currently checking, maybe someone else has an answer, but when i remove your exception handling, I get other errors that might be causing issues. – glls May 26 '16 at 01:04
  • Can you paste the error here? Because I removed the try/except statement and I didn't get any error, beside the >20 glitch! @glls – Elie khalifeh May 26 '16 at 01:14