472,364 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 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 3964

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: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

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.