1
from Tkinter import *

root = Tk()

Austria = StringVar()
label = Label(root, textvariable = Austria)

Austria.set("Austria")
label.pack(side = LEFT)

Cap_Austria = Entry(root)
Cap_Austria.pack(side = RIGHT)

root.mainloop()

I am making an EU quiz but this time it is graphical. I would like to make the entry in the entry box a variable so that I can do something along the lines of:

if entry_string_austria == Vienna:
   #It's correct

I have researched the get() function at the sites below but I don't understand most of the documentation.

http://mail.python.org/pipermail/tkinter-discuss/2008-June/001447.html http://effbot.org/tkinterbook/text.htm

Another stackoverflow question which is very similar but I still don't understand

http://mail.python.org/pipermail/tutor/2005-February/035669.html http://bytes.com/topic/python/answers/761497-using-get-entry-tkinter

Community
  • 1
  • 1
shibambu
  • 55
  • 4
  • Do you have a question in particular? From your research, I'm guessing you know that calling `get` will return the text of the `Entry` widget as a string. So what's the problem? – Kevin Aug 02 '13 at 11:54

1 Answers1

2

You're right about using get()!

The syntax in this case would be

City = Cap_Austria.get()

if City == 'Vienna':
    #do stuff

You can make default text:

Cap_Austria.insert(0,  'default text')

And even follow this template:

class EntryTemplate(Entry):
    def __init__(self, master, status):
        Entry.__init__(self, master)
        Entry.insert(self, 0, status)
        Entry.bind(self, "<FocusIn>", lambda event: self.clickOnEntry(event, status))
        Entry.bind(self, "<FocusOut>", lambda event: self.clickOffEntry(event, status))


    def clickOnEntry(self, event, defaultText):
        if self.get() == defaultText:
            self.delete(0, END)

    def clickOffEntry(self, event, defaultText):
        if len(self.get()) == 0:
            Entry.insert(self, 0, defaultText)

That you would use like this:

Cap_Austria = EntryTemplate(root, 'default text')
Cap_Austria.pack(side = RIGHT)

City = Cap_Austria.get()

To make it so that there's not only a default text but if you click on it it goes away and if you click away without having entered anything it comes back. Good luck!

Al.Sal
  • 984
  • 8
  • 19