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

Getting value from Tkinter entry widget: variable scope difficulty

I'm trying to construct a simple Tkinter GUI and I'm having trouble with
getting the value of an entry widget and passing it onto a callback
function. But I'm not grokking variable scope correctly.

I have two functions. Here are the relevant excerpts:

def makeGUI():

###lots of code to set up toplevel windows

searchbutton=Button(topframe, image=searchglass, command=doSomething)
searchbutton.image=searchglass
searchbutton.pack(side=LEFT, expand = NO)

searchterm=StringVar()

entryterm=Entry(topframe, textvariable=searchterm)
entryterm.pack(side=RIGHT)

entrylabel=Label(topframe, text="Search:")
entrylabel.pack(side=RIGHT)
def doSomething():

file = os.popen('systemcommand %s' % searchterm, 'r')

print searchterm

---

What I want to happen is the value of searchterm to be passed on to the
doSomething() call when the searchbutton is pressed. Right now it
appears the value of "searchterm" is local to each function.

In Tcl this would be handled by simply assigning the variable
"searchterm" to the global namespace in each function, i.e. "global
searchterm." I've looked and can't quite figure out how to do this in
Python. Any advice is appreciated.

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Nov 20 '06 #1
2 4079

Kevin,
if the present value of searchterm is what you
want, I suggest you make a class
class Kgui:
def __init__(self) : # init self
self.makeGUI()
def makeGUI(self):

###lots of code to set up toplevel windows

searchbutton=Button(topframe,
image=searchglass, command=self.doSomething)
searchbutton.image=searchglass
searchbutton.pack(side=LEFT, expand = NO)

# and all the resrt of the code
def doSomething(self):

file = os.popen('systemcommand %s' %
searchterm, 'r')

self.searchterm = searchterm

print self.searchterm #used inside the class

Kgui.searchterm = searchterm

print Kgui.searchterm #used outside the class


you can also do;
Kgui.searchterm = self.searchterm = searchterm

I think this is what you are asking for.

jim-on-linux

http://www.inqvista.com


On Sunday 19 November 2006 21:18, Kevin Walzer
wrote:
I'm trying to construct a simple Tkinter GUI
and I'm having trouble with getting the value
of an entry widget and passing it onto a
callback function. But I'm not grokking
variable scope correctly.

I have two functions. Here are the relevant
excerpts:

def makeGUI():

###lots of code to set up toplevel windows

searchbutton=Button(topframe,
image=searchglass, command=doSomething)
searchbutton.image=searchglass
searchbutton.pack(side=LEFT, expand = NO)

searchterm=StringVar()

entryterm=Entry(topframe,
textvariable=searchterm)
entryterm.pack(side=RIGHT)

entrylabel=Label(topframe, text="Search:")
entrylabel.pack(side=RIGHT)
def doSomething():

file = os.popen('systemcommand %s' %
searchterm, 'r')

print searchterm

---

What I want to happen is the value of
searchterm to be passed on to the doSomething()
call when the searchbutton is pressed. Right
now it appears the value of "searchterm" is
local to each function.

In Tcl this would be handled by simply
assigning the variable "searchterm" to the
global namespace in each function, i.e. "global
searchterm." I've looked and can't quite figure
out how to do this in Python. Any advice is
appreciated.

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Nov 20 '06 #2
"Kevin Walzer" <kw@kevin-walzer.com>
I'm trying to construct a simple Tkinter GUI and I'm having trouble with
getting the value of an entry widget and passing it onto a callback
function. But I'm not grokking variable scope correctly.

I have two functions. Here are the relevant excerpts:

def makeGUI():

###lots of code to set up toplevel windows

searchbutton=Button(topframe, image=searchglass, command=doSomething)
searchbutton.image=searchglass
searchbutton.pack(side=LEFT, expand = NO)

searchterm=StringVar()
this will make a new local variable - if you want to assign to the global one,
you need to add a global statement before doing this, in this makeGUI function.
- you can read the global here if you have defined searchterm in an outer scope,
but the assign will make a new local one.
>
entryterm=Entry(topframe, textvariable=searchterm)
entryterm.pack(side=RIGHT)

entrylabel=Label(topframe, text="Search:")
entrylabel.pack(side=RIGHT)
def doSomething():

file = os.popen('systemcommand %s' % searchterm, 'r')
here searchterm seems to be a command line variable.
look in the help for sys.argv how to access command line variables

- not that you need to, but its good to know... :-)

had makeGUI been a class instead of a function, you could have
written makeGUI.searchterm here, without mucking around with globals....
>
print searchterm

---

What I want to happen is the value of searchterm to be passed on to the
doSomething() call when the searchbutton is pressed. Right now it
appears the value of "searchterm" is local to each function.
True
In Tcl this would be handled by simply assigning the variable
"searchterm" to the global namespace in each function, i.e. "global
searchterm." I've looked and can't quite figure out how to do this in
Python. Any advice is appreciated.
global searchterm # - in outer scope

global searchterm # - in inner scope before assign
HTH - Hendrik
Nov 20 '06 #3

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

Similar topics

6
by: Elaine Jackson | last post by:
I've got a script where a button gets pushed over and over: to cut down on the carpal tunnel syndrome I'd like to have the button respond to presses of the Enter key as well as mouse clicks; can...
8
by: VK | last post by:
Hi! What I'm missing in following code? Cannot get the values of radiobuttons. Starting only one class (GetVariant), it works. When I put two classes together, it doesn't. Regards, VK from...
7
by: William Gill | last post by:
Is there a simple way to cut and paste from a tkinter text widget to an entry widget? I know I could create a mouse button event that triggers a popup (message widget) prompting for cut/paste in...
3
by: Matt Hammond | last post by:
Here's a strange one in Tkinter that has me stumped: (I'm running python 2.4 on Suse Linux 9.3 64bit) I'm trying to make a set of Entry widgets with Label widgets to the left of each one, using...
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...
5
by: vagrantbrad | last post by:
I've created a short test program that uses tkFileDialog.askdirectory to help the user input a path into a tk entry widget. The problem I'm having is that when I run the code as listed below, the...
3
Elias Alhanatis
by: Elias Alhanatis | last post by:
Hello to everybody!! I am running Python 2.5.1 on Windows Vista and i have a problem with the "Entry" widget of Tkinter. Take a look at this code: from Tkinter import * def fetch(): ...
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...
0
by: lee.walczak | last post by:
Hi, I hope someone can help here. I have made lots of progress implementing a gui application which at present uses the TableList python wrapper to interface with Tcl TableList implemention. ...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.