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: - resistanceEntry = Spinbox(resistorFrame, from_=1, to=999, width=10, wrap=True, validate=ALL, validatecommand=validate)
-
-
def validate():
-
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
12 8579
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: - resistanceEntry = Spinbox(resistorFrame, from_=1, to=999, width=10, wrap=True, validate=ALL, validatecommand=validate)
-
-
def validate():
-
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.
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: - resistanceEntry = Spinbox(resistorFrame, from_=1, to=999, width=10, wrap=True, validate=ALL, validatecommand=validate)
-
-
def validate():
-
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: - resistanceEntry = Spinbox(resistorFrame, from_=1, to=999, width=10, wrap=True, validate=ALL, validatecommand=validate)
-
-
def validate():
-
print "VALIDATE"
-
return True
Perhaps returning None is causing the problem.
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?
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 -
-
def validate(input)
-
for i in range (1000):
-
if i == input:
-
return True
-
(...etc...)
-
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 -
-
def validate(input)
-
for i in range (1000):
-
if i == input:
-
return True
-
(...etc...)
-
Something like this: - >>> def validate(v):
-
... if isinstance(v, int) and v > 0 and v < 1000:
-
... return True
-
... else:
-
... return False
-
...
-
>>> validate(6)
-
True
-
>>> validate(0)
-
False
-
>>> validate(999)
-
True
-
>>> validate(1000)
-
False
-
>>> if validate(v):
-
... print 'Do some stuff'
-
...
-
Do some stuff
-
>>>
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: - 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 - resistanceEntry.set(value)
or you can might be able to - STARTVALUE = 100 # or whatever
-
resistanceEntry = Spinbox(resistorFrame, from_=1, to=999,
-
value=STARTVALUE, width=10, wrap=True,
-
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???
In the Frame's __init__(), keep a reference: - 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 - resistanceEntry.set(value)
or you can might be able to - STARTVALUE = 100 # or whatever
-
resistanceEntry = Spinbox(resistorFrame, from_=1, to=999,
-
value=STARTVALUE, width=10, wrap=True,
-
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!
Something like this: - >>> def validate(v):
-
... if isinstance(v, int) and v > 0 and v < 1000:
-
... return True
-
... else:
-
... return False
-
...
-
>>> validate(6)
-
True
-
>>> validate(0)
-
False
-
>>> validate(999)
-
True
-
>>> validate(1000)
-
False
-
>>> if validate(v):
-
... print 'Do some stuff'
-
...
-
Do some stuff
-
>>>
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? -
resistorProperties.resistanceEntry = Spinbox(resistorProperties.resistorFrame, from_=1, to=999, width=10, wrap=True, validate="all", validatecommand=validate) #use get() to find entry
-
resistorProperties.resistanceEntry.grid(column=1, row=1, sticky=N+E)
-
-
def validate():
-
print "VALIDATE"
-
-
if resistorProperties.resistanceEntry == None:
-
return True
-
else:
-
toValidate = resistorProperties.resistanceEntry.get()
-
-
print toValidate
-
return True
-
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 :)
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: -
-
class ResistorModel(Frame):
-
"""A subclass of Tkinter.Frame for modeling a resistor using IntVar."""
-
def __init__(self, root, powerRating, *args, **kwargs):
-
Frame.__init__(self, root, *args, **kwargs)
-
self.powerRating = powerRating
-
self.Ohms = IntVar()
-
self.Ohms.trace('w', self.validateOhms)
-
-
## label = Label()
-
-
self.resistanceEntry = Spinbox(self, from_=1, to=999, width=10, wrap=True,
-
validate="all", textvariable=self.Ohms) #use get() to find entry
-
## self.resistanceEntry.config(validatecommand=self.validateOhms)
-
self.resistanceEntry.grid(column=1, row=1, sticky=N+E)
-
-
def validateOhms(self, *args):
-
print "VALIDATE OHMS"
-
toValidate = self.Ohms.get()
-
-
print toValidate, type(toValidate)
-
return True
-
-
-
if __name__ == "__main__":
-
root = Tk()
-
-
frame = ResistorModel(root, 10)
-
frame.pack()
-
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: -
-
class ResistorModel(Frame):
-
"""A subclass of Tkinter.Frame for modeling a resistor using IntVar."""
-
def __init__(self, root, powerRating, *args, **kwargs):
-
Frame.__init__(self, root, *args, **kwargs)
-
self.powerRating = powerRating
-
self.Ohms = IntVar()
-
self.Ohms.trace('w', self.validateOhms)
-
-
## label = Label()
-
-
self.resistanceEntry = Spinbox(self, from_=1, to=999, width=10, wrap=True,
-
validate="all", textvariable=self.Ohms) #use get() to find entry
-
## self.resistanceEntry.config(validatecommand=self.validateOhms)
-
self.resistanceEntry.grid(column=1, row=1, sticky=N+E)
-
-
def validateOhms(self, *args):
-
print "VALIDATE OHMS"
-
toValidate = self.Ohms.get()
-
-
print toValidate, type(toValidate)
-
return True
-
-
-
if __name__ == "__main__":
-
root = Tk()
-
-
frame = ResistorModel(root, 10)
-
frame.pack()
-
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
:)
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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 =...
|
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! :-( ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
| |