0
  • I created a simple spin box. what I want is, as I increase or decrease the value in spin box, a new entry should be created or deleted respectively.
  • I am not able to get the Int value from the spinbox.
  • I tried, user_input = int(sb.get()), But this also didn't work.
  • I am getting this error, 'str' object cannot be interpreted as an integer.

,,,

1st code:

from tkinter import *
root = Tk()

sb_var = IntVar()
sb = Spinbox(root, bg='gray', textvariable=sb_var)
sb.pack()
user_input = sb.get()

for x in range(user_input):
    my_entry = Entry(root)
    my_entry.pack()

root.mainloop()

,,,

2nd code:

from tkinter import *
root = Tk()
root.geometry("500x400")


def up_or_down(direction):
    if direction == "up":
        my_entry = Entry(root)
        my_entry.pack()

    if direction == "down":         # I don't know how to code this condition
        my_entry.destroy()

tcl_up_or_down = root.register(up_or_down)

sb = Spinbox(root, from_=0, to=100, bg='gray', command=(tcl_up_or_down, '%d'))
sb.pack()

root.mainloop()

'''

  • you're getting the value and creating the entry widgets about a millisecond after creating the spinbox. The user won't have seen the UI, much less have a chance to interact with it. – Bryan Oakley Jan 22 '21 at 05:32
  • Thanks for the reply Bryan, but why my entry widget isn't visible in GUI ? – Milind Khobragade Jan 23 '21 at 12:53
  • Because `mainloop` hasn't started before that code runs. – Bryan Oakley Jan 23 '21 at 13:59
  • Ok, if that is the problem, then what is the solution? – Milind Khobragade Jan 25 '21 at 18:31
  • The solution is hard to say as it depends on what you're trying to accomnplish. In short, move the code into a function and then call the function after the user has changed the spinbox. – Bryan Oakley Jan 25 '21 at 18:37
  • I mentioned what I am trying to accomplish in my 1st point. – Milind Khobragade Jan 27 '21 at 07:05
  • Well, yes, you've said what you want that one line to accomplish. My point is, if I increase the value to 10, you want 10 entries to be added. If I then change it to 5, is it supposed to remove five of the existing entries or add five more? Or, do you want to wait until the user is done changing it, and then only add the entries once they press return or click a button? – Bryan Oakley Jan 27 '21 at 14:39
  • Hey thanks Bryan for having patience. Please look at the 2nd code example I came up with. As user click up button, 1 entry is created at a time. I want if user click down button, last entry should be deleted. If user click down button again, another last entry should be deleted. (One last entry should be deleted each time if user clicks on down button.) – Milind Khobragade Jan 27 '21 at 19:48

0 Answers0