By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

Tkinter Spinbox

P: 7
Hi

I need a spinbox which when created can read a variable (which is an integer) from another location and use that variable for the starting value. i also need to only allow integers be input into the text field by keyboard (and only has high as 999). lastly, whenever the spinbox value is changed (either by keyboard input or by clicking on the arrows) i need to be able to verify that it is an integer and then save its value into another location.

i have tried to do these things but without success. currently i have this (it has been cut out of my main program:

Expand|Select|Wrap|Line Numbers
  1.  resistanceEntry = Spinbox(resistorFrame, from_=1, to=999, width=10, wrap=True, validate=ALL, validatecommand=validate)
  2.  
  3. def validate():
  4.     print "VALIDATE" 
i noticed that even with validate set to ALL it only runs the command validate once and that is when the spinbox is first created.

any help would be greatly appreciated, but please try and keep it simple i am still very new to this

thanks
May 5 '07 #1
Share this Question
Share on Google+
12 Replies


bartonc
Expert 5K+
P: 6,596
Hi

I need a spinbox which when created can read a variable (which is an integer) from another location and use that variable for the starting value. i also need to only allow integers be input into the text field by keyboard (and only has high as 999). lastly, whenever the spinbox value is changed (either by keyboard input or by clicking on the arrows) i need to be able to verify that it is an integer and then save its value into another location.

i have tried to do these things but without success. currently i have this (it has been cut out of my main program:

Expand|Select|Wrap|Line Numbers
  1.  resistanceEntry = Spinbox(resistorFrame, from_=1, to=999, width=10, wrap=True, validate=ALL, validatecommand=validate)
  2.  
  3. def validate():
  4.     print "VALIDATE" 
i noticed that even with validate set to ALL it only runs the command validate once and that is when the spinbox is first created.

any help would be greatly appreciated, but please try and keep it simple i am still very new to this

thanks
Electronics huh?
I'm guessing that you need to associate a Variable class object that gets changes. I'll have a look tonight. Got to go at the moment.
May 5 '07 #2

bartonc
Expert 5K+
P: 6,596
Hi

I need a spinbox which when created can read a variable (which is an integer) from another location and use that variable for the starting value. i also need to only allow integers be input into the text field by keyboard (and only has high as 999). lastly, whenever the spinbox value is changed (either by keyboard input or by clicking on the arrows) i need to be able to verify that it is an integer and then save its value into another location.

i have tried to do these things but without success. currently i have this (it has been cut out of my main program:

Expand|Select|Wrap|Line Numbers
  1.  resistanceEntry = Spinbox(resistorFrame, from_=1, to=999, width=10, wrap=True, validate=ALL, validatecommand=validate)
  2.  
  3. def validate():
  4.     print "VALIDATE" 
i noticed that even with validate set to ALL it only runs the command validate once and that is when the spinbox is first created.

any help would be greatly appreciated, but please try and keep it simple i am still very new to this

thanks
Nice job on the CODE tags. Thanks. I like you naming style, too.

Validators must return True or False:
Expand|Select|Wrap|Line Numbers
  1.  resistanceEntry = Spinbox(resistorFrame, from_=1, to=999, width=10, wrap=True, validate=ALL, validatecommand=validate)
  2.  
  3. def validate():
  4.     print "VALIDATE" 
  5.     return True
Perhaps returning None is causing the problem.
May 5 '07 #3

P: 7
thank you so much :) when i return true then it validates every time, now all i need is get the spinbox value to start at a specific value, any ideas?
May 6 '07 #4

P: 7
EDIT: also how would i write the validate function i had the idea of this (below) but it seems very inefficient there must be a better way

Expand|Select|Wrap|Line Numbers
  1.  
  2. def validate(input)
  3.    for i in range (1000):
  4.       if i == input:
  5.          return True
  6.    (...etc...)
  7.  
May 6 '07 #5

bvdet
Expert Mod 2.5K+
P: 2,851
EDIT: also how would i write the validate function i had the idea of this (below) but it seems very inefficient there must be a better way

Expand|Select|Wrap|Line Numbers
  1.  
  2. def validate(input)
  3.    for i in range (1000):
  4.       if i == input:
  5.          return True
  6.    (...etc...)
  7.  
Something like this:
Expand|Select|Wrap|Line Numbers
  1. >>> def validate(v):
  2. ...     if isinstance(v, int) and v > 0 and v < 1000:
  3. ...         return True
  4. ...     else:
  5. ...         return False
  6. ...     
  7. >>> validate(6)
  8. True
  9. >>> validate(0)
  10. False
  11. >>> validate(999)
  12. True
  13. >>> validate(1000)
  14. False
  15. >>> if validate(v):
  16. ...     print 'Do some stuff'
  17. ...     
  18. Do some stuff
  19. >>> 
May 6 '07 #6

bartonc
Expert 5K+
P: 6,596
thank you so much :) when i return true then it validates every time, now all i need is get the spinbox value to start at a specific value, any ideas?
In the Frame's __init__(), keep a reference:
Expand|Select|Wrap|Line Numbers
  1. self.resistanceEntry = Spinbox(resistorFrame, from_=1, to=999, width=10, wrap=True, validate=ALL, validatecommand=validate)
Then, any time you want to, the a Frame method can
Expand|Select|Wrap|Line Numbers
  1. resistanceEntry.set(value)
or you can might be able to
Expand|Select|Wrap|Line Numbers
  1. STARTVALUE = 100   # or whatever
  2. resistanceEntry = Spinbox(resistorFrame, from_=1, to=999,
  3.                           value=STARTVALUE, width=10, wrap=True,
  4.                           validate=ALL, validatecommand=validate)
Of, course, you'll have to experiment because I have not tested this.

Do you know where to find/have the on-line Tkinter reference???
May 6 '07 #7

bartonc
Expert 5K+
P: 6,596
In the Frame's __init__(), keep a reference:
Expand|Select|Wrap|Line Numbers
  1. self.resistanceEntry = Spinbox(resistorFrame, from_=1, to=999, width=10, wrap=True, validate=ALL, validatecommand=validate)
Then, any time you want to, the a Frame method can
Expand|Select|Wrap|Line Numbers
  1. resistanceEntry.set(value)
or you can might be able to
Expand|Select|Wrap|Line Numbers
  1. STARTVALUE = 100   # or whatever
  2. resistanceEntry = Spinbox(resistorFrame, from_=1, to=999,
  3.                           value=STARTVALUE, width=10, wrap=True,
  4.                           validate=ALL, validatecommand=validate)
Of, course, you'll have to experiment because I have not tested this.

Do you know where to find/have the on-line Tkinter reference???
Actually, I think that the proper way to do this is with and IntVar. There is a discussion here.

The Tkinter (old and incomplete) reference is here.

We'll make a Pythoneer out of you, yet!
May 6 '07 #8

P: 7
Something like this:
Expand|Select|Wrap|Line Numbers
  1. >>> def validate(v):
  2. ...     if isinstance(v, int) and v > 0 and v < 1000:
  3. ...         return True
  4. ...     else:
  5. ...         return False
  6. ...     
  7. >>> validate(6)
  8. True
  9. >>> validate(0)
  10. False
  11. >>> validate(999)
  12. True
  13. >>> validate(1000)
  14. False
  15. >>> if validate(v):
  16. ...     print 'Do some stuff'
  17. ...     
  18. Do some stuff
  19. >>> 
this is perfect except how would you write isinstance? (its not a built in is it?)

i have taken in all of the other things people have written and just have one more question: i noticed that if i use the validatecommand function as used below then the validation is always one step behind. ie if the spinbox contains 5 and i change it to 6 it will verify the old value of 5 and then change the spinbox value of 6, thus it is always one step behind. is there any way to fix this or is there a way around it?

Expand|Select|Wrap|Line Numbers
  1. resistorProperties.resistanceEntry = Spinbox(resistorProperties.resistorFrame, from_=1, to=999, width=10, wrap=True, validate="all", validatecommand=validate)  #use get() to find entry
  2.     resistorProperties.resistanceEntry.grid(column=1, row=1, sticky=N+E)
  3.  
  4. def validate():
  5.     print "VALIDATE"
  6.  
  7.     if resistorProperties.resistanceEntry == None:
  8.         return True
  9.     else:
  10.         toValidate = resistorProperties.resistanceEntry.get()
  11.  
  12.     print toValidate   
  13.     return True
  14.  
May 7 '07 #9

P: 7
this is perfect except how would you write isinstance? (its not a built in is it?)
worked that part out, only the second part applies :)
May 7 '07 #10

bartonc
Expert 5K+
P: 6,596
worked that part out, only the second part applies :)
I fixed the event-sequence problem using an IntVar and trace().
I will ask you to consider designing you resistor model as a subclass of Frame, as follows:
Expand|Select|Wrap|Line Numbers
  1.  
  2. class ResistorModel(Frame):
  3.     """A subclass of Tkinter.Frame for modeling a resistor using IntVar."""
  4.     def __init__(self, root, powerRating, *args, **kwargs):
  5.         Frame.__init__(self, root, *args, **kwargs)
  6.         self.powerRating = powerRating
  7.         self.Ohms = IntVar()
  8.         self.Ohms.trace('w', self.validateOhms)
  9.  
  10. ##        label = Label()
  11.  
  12.         self.resistanceEntry = Spinbox(self, from_=1, to=999, width=10, wrap=True,
  13.                                        validate="all", textvariable=self.Ohms)  #use get() to find entry
  14. ##        self.resistanceEntry.config(validatecommand=self.validateOhms)
  15.         self.resistanceEntry.grid(column=1, row=1, sticky=N+E)
  16.  
  17.     def validateOhms(self, *args):
  18.         print "VALIDATE OHMS"
  19.         toValidate = self.Ohms.get()
  20.  
  21.         print toValidate, type(toValidate)
  22.         return True
  23.  
  24.  
  25. if __name__ == "__main__":
  26.     root = Tk()
  27.  
  28.     frame = ResistorModel(root, 10)
  29.     frame.pack()
  30.  
May 7 '07 #11

P: 7
I fixed the event-sequence problem using an IntVar and trace().
I will ask you to consider designing you resistor model as a subclass of Frame, as follows:
Expand|Select|Wrap|Line Numbers
  1.  
  2. class ResistorModel(Frame):
  3.     """A subclass of Tkinter.Frame for modeling a resistor using IntVar."""
  4.     def __init__(self, root, powerRating, *args, **kwargs):
  5.         Frame.__init__(self, root, *args, **kwargs)
  6.         self.powerRating = powerRating
  7.         self.Ohms = IntVar()
  8.         self.Ohms.trace('w', self.validateOhms)
  9.  
  10. ##        label = Label()
  11.  
  12.         self.resistanceEntry = Spinbox(self, from_=1, to=999, width=10, wrap=True,
  13.                                        validate="all", textvariable=self.Ohms)  #use get() to find entry
  14. ##        self.resistanceEntry.config(validatecommand=self.validateOhms)
  15.         self.resistanceEntry.grid(column=1, row=1, sticky=N+E)
  16.  
  17.     def validateOhms(self, *args):
  18.         print "VALIDATE OHMS"
  19.         toValidate = self.Ohms.get()
  20.  
  21.         print toValidate, type(toValidate)
  22.         return True
  23.  
  24.  
  25. if __name__ == "__main__":
  26.     root = Tk()
  27.  
  28.     frame = ResistorModel(root, 10)
  29.     frame.pack()
  30.  

wow it works :) thank you so much your code is fantastic and with minimal modification this can easily be placed directly into my program.

i apologise for the late reply but i have been swamped over the last few weeks and have not had any time to work on my program

:)
May 18 '07 #12

bartonc
Expert 5K+
P: 6,596
wow it works :) thank you so much your code is fantastic and with minimal modification this can easily be placed directly into my program.

i apologise for the late reply but i have been swamped over the last few weeks and have not had any time to work on my program

:)
I was just wondering about you today. Thank you for the update.
Keep posting, (like some working code, maybe)
Barton
May 18 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.