473,387 Members | 1,779 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,387 software developers and data experts.

Tkinter StringVar mystery

First off I have this class (thanks to whoever came up with this way back
when):

######################
# BEGIN: class Command
# LIB:Command():2006.110
# Pass arguments to functions from button presses and menu selections,
# bind's. Nice!
# In your declaration:
# ...command = Command(func, args,...)
class Command:
def __init__(self, func, *args, **kw):
self.func = func
self.args = args
self.kw = kw
def __call__(self, *args, **kw):
args = self.args+args
kw.update(self.kw)
apply(self.func, args, kw)
# END: class Command
Then in the setup part of an entry form I have:

# CHBCBarcodeLastVar is the variable for an Entry() field
Button(SubFrame, text = "Print Barcode", \
command = Command(test, "other", CHBCBarcodeLastVar.get(), \
"CHBC")).pack(side = LEFT)
Then this is a/the test function:

def test(What, BC, Where, e = None):
# Does print the correct value
print CHBCBarcodeLastVar.get()
# Does change the field on the form
CHBCBarcodeLastVar.set("55555")
# BC is ""
print ":"+What+":", ":"+BC+":", ":"+Where+":"
return

Everything works as it should, except when the Button is clicked BC is an
empty str in test(). How come? (I really have NO clue how that Command
class works, but I use it like crazy. Is it the problem?)

Thanks!

Bob
Jul 17 '06 #1
1 2389
On Mon, 2006-07-17 at 15:00 -0600, Bob Greschke wrote:
First off I have this class (thanks to whoever came up with this way back
when):

######################
# BEGIN: class Command
# LIB:Command():2006.110
# Pass arguments to functions from button presses and menu selections,
# bind's. Nice!
# In your declaration:
# ...command = Command(func, args,...)
class Command:
def __init__(self, func, *args, **kw):
self.func = func
self.args = args
self.kw = kw
def __call__(self, *args, **kw):
args = self.args+args
kw.update(self.kw)
apply(self.func, args, kw)
# END: class Command
Then in the setup part of an entry form I have:

# CHBCBarcodeLastVar is the variable for an Entry() field
Button(SubFrame, text = "Print Barcode", \
command = Command(test, "other", CHBCBarcodeLastVar.get(), \
"CHBC")).pack(side = LEFT)
Then this is a/the test function:

def test(What, BC, Where, e = None):
# Does print the correct value
print CHBCBarcodeLastVar.get()
# Does change the field on the form
CHBCBarcodeLastVar.set("55555")
# BC is ""
print ":"+What+":", ":"+BC+":", ":"+Where+":"
return

Everything works as it should, except when the Button is clicked BC is an
empty str in test(). How come? (I really have NO clue how that Command
class works, but I use it like crazy. Is it the problem?)
The problem is when you are creating the "Print Barcode" button. Think
about what the value of CHBCBarcodeLastVar.get() is when you create the
button ? I bet it is an empty string. This value will always be passed
to test, regardless of how it changes in the future. What you probably
want is to pass the StringVar reference and then do a get in test.

Eg:

# CHBCBarcodeLastVar is the variable for an Entry() field
Button(SubFrame, text = "Print Barcode", \
command = Command(test, "other", CHBCBarcodeLastVar, \
"CHBC")).pack(side = LEFT)

def test(What, BC, Where, e = None):
print ":"+What+":", ":"+BC.get()":", ":"+Where+":"
return

Regards,

John

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Jul 17 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stewart Midwinter | last post by:
I would like to link the contents of three OptionMenu lists. When I select an item from the first list (call it continents), the contents of the 2nd list (call it countries) would update. And in...
1
by: Club-B42 | last post by:
when i start opt_newlogin.py directly it works fine(outputs '1 1 1 1'), but if i start it from options.py there is an error(outputs ''). ======== opt_newlogin.py ======== from config import *...
0
by: Michele Simionato | last post by:
I am in the process of learning pygtk and I would like to port some custom made Tkinter widgets to pygtk, just an exercise. For instance I have this code: .. from Tkinter import * .. .. class...
5
by: max(01)* | last post by:
hello. the following code: 1 from Tkinter import * 2 3 class MiaApp: 4 def __init__(self, genitore): 5 self.mioGenitore = genitore 6 self.i = IntVar()
3
by: William Gill | last post by:
Working with tkinter, I have a createWidgets() method in a class. Within createWidgets() I create several StringVars() and assign them to the textvariable option of several widgets. Effectively my...
1
by: Jo Schambach | last post by:
I want to build an array of entry widgets in python with Tkinter that all have similar textvariables. I was hoping that I could use an array of StringVar variables to attach to these widgets, so...
4
by: Kevin Walzer | last post by:
I'm trying to manage user preferences in a Tkinter application by initializing some values that can then be configured from a GUI. The values are set up as a dict, like so: self.prefs= {...
8
by: Lie | last post by:
Inspect the following code: --- start of code --- import Tkinter as Tk from Tkconstants import * root = Tk.Tk() e1 = Tk.Entry(root, text = 'Hello World') e2 = Tk.Entry(root, text = 'Hello...
3
by: seanacais | last post by:
I'm trying to build an unknown number of repeating gui elements dynamically so I need to store the variables in a list of dictionaries. I understand that Scale "variable" name needs to be a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.