[Tutor] newbie TK question

Richard Chamberlain richard_chamberlain@ntlworld.com
Sun, 23 Jul 2000 18:44:49 +0100


Hi Joseph,

from Tkinter import *
root=Tk()
myStringVar=StringVar()
entry=Entry(root,textvariable=myStringVar,width=30)
def entryReturn(event):
    str=myStringVar.get()
    print str
    myStringVar.set('')
entry.bind('<Return>',entryReturn)
entry.pack()
root.mainloop()

Entry (and a few other widgets) have a textvariable option to which you give
an instance of StringVar. After that your instance (in the case above
myStringVar) has a set and a get method. So I bind a <Return> event to the
entryReturn method and then use .get() to get the contents of the entry.

Richard
----- Original Message -----
From: Joseph Stubenrauch <nothingisgoingtochangemyworld@yahoo.com>
To: <tutor@python.org>
Sent: Sunday, July 23, 2000 6:06 PM
Subject: [Tutor] newbie TK question


> Hello,
>
> I am sure that the answer is sitting right under my
> nose, but I've been rummaging through the oneline
> Tkinter documents and can't find a straight answer or
> good example.  The solution, I have a feeling, is
> simple.
>
> I am trying to create an "Entry" widget that takes its
> contents and puts them in a string whenever the user
> hits the enter key (or perhaps a submit button, but I
> am hoping for enter).
>
> In other words, if the user types "wakka wakka" into
> the Entry widget at hits enter, it creates a string
> (named answer for example) that equals wakka wakka.
>
> The Tk tutorial page mentions get() => string but I'm
> not quite sure how to incorporate that and how to make
> it a command that is triggered by a button press or
> the enter key.
>
> Thanks in advance for the input!
>
> Newbiely,
> Joe
>
> __________________________________________________
> Do You Yahoo!?
> Get Yahoo! Mail - Free email you can access from anywhere!
> http://mail.yahoo.com/
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor