473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Tkinter Spinbox

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
12 8579
bartonc
6,596 Expert 4TB
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
6,596 Expert 4TB
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
5N1P3R
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
5N1P3R
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
2,851 Expert Mod 2GB
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
6,596 Expert 4TB
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
6,596 Expert 4TB
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
5N1P3R
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
5N1P3R
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
6,596 Expert 4TB
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
5N1P3R
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
6,596 Expert 4TB
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

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

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
3
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter...
1
by: Tony Eva | last post by:
The spinbox widget in Tk 8.4 has an option to allow a command to be specified that is invoked when either the up or down buttons of the spinbox are pressed, as in: ... self.sb =...
0
by: Askari | last post by:
Hi, I search on google a example for Spinbox (with Tkinter module). Can't find! If someone have a basic example for with a Spinbox, send me him! Because? I don't know how use this widget! :-( ...
0
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
1
by: aft3rgl0w | last post by:
hello everyone, i am trying to create an activex clock control, which can display the current time or be used to set a time in a control. i am using Visual Basic 2005, with 3 numericupdown...
8
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to...
3
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.