simple Tkinter Question

M

Maboroshi

Hi I was curious how I can pass arguments through a Tkinter Entry widget

say I have an Entry widget and a Button widget I enter text into the
entry and now I want the button to process the text is there a certain
command I have to use to assign the button to the entry or vise versa

anyhelp appreciated
 
E

Eric Brunel

Maboroshi said:
Hi I was curious how I can pass arguments through a Tkinter Entry widget

say I have an Entry widget and a Button widget I enter text into the
entry and now I want the button to process the text is there a certain
command I have to use to assign the button to the entry or vise versa

anyhelp appreciated

Sorry, but you'll have to be a bit more specific than that. What is exactly your
problem? Getting and setting the text in the entry? Assigning an action to the
button? What do you mean exactly by "assign the button to the entry"?
 
D

duikboot

Maboroshi said:
Hi I was curious how I can pass arguments through a Tkinter Entry widget

say I have an Entry widget and a Button widget I enter text into the
entry and now I want the button to process the text is there a certain
command I have to use to assign the button to the entry or vise versa

anyhelp appreciated

Take a look at
http://www.pythonware.com/library/tkinter/introduction/index.htm

example

from Tkinter import *

def pr_text():
text = entry.get()
print text

gui=Tk()
entry=Entry(gui)
entry.pack()
button=Button(gui, text="print", command=pr_text)
button.pack()

gui.mainloop()
 
E

Eric Brunel

duikboot said:
Take a look at
http://www.pythonware.com/library/tkinter/introduction/index.htm

example

from Tkinter import *

def pr_text():
text = entry.get()
print text

gui=Tk()
entry=Entry(gui)
entry.pack()
button=Button(gui, text="print", command=pr_text)
button.pack()

gui.mainloop()

If *that* is the actual problem, I'd use a Tkinter StringVariable to do the job:

from Tkinter import *
root = Tk()
v = StringVar()
Entry(root, textvariable=v).pack()
def pr_text():
print v.get()
Button(root, text='Print', command=pr_text).pack()
root.mainloop()

Tkinter variables are a better choice because you often want to know what *was*
in your entry after it has been destroyed from the display. In such a case,
entry.get() will raise a TclError, because the entry no more exists. If you use
a variable, it will still be there after the entry was destroyed, so you can
still read its value.

HTH
 
D

duikboot

Eric said:
If *that* is the actual problem, I'd use a Tkinter StringVariable to do
the job:

from Tkinter import *
root = Tk()
v = StringVar()
Entry(root, textvariable=v).pack()
def pr_text():
print v.get()
Button(root, text='Print', command=pr_text).pack()
root.mainloop()

Tkinter variables are a better choice because you often want to know
what *was* in your entry after it has been destroyed from the display.
In such a case, entry.get() will raise a TclError, because the entry no
more exists. If you use a variable, it will still be there after the
entry was destroyed, so you can still read its value.

HTH

You're absolutely right. It's better indeed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top