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()
我正在进行欧盟测验,但这次是图形化的。我想在输入框中输入一个变量,以便我可以执行以下操作:
if entry_string_austria == Vienna:
#It's correct
我在下面的网站上研究了get()函数,但我不了解大部分文档。
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
答案 0 :(得分:2)
你使用get()
是正确的!
本例中的语法是
City = Cap_Austria.get()
if City == 'Vienna':
#do stuff
您可以制作默认文字:
Cap_Austria.insert(0, 'default text')
甚至可以按照这个模板:
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)
你会这样使用:
Cap_Austria = EntryTemplate(root, 'default text')
Cap_Austria.pack(side = RIGHT)
City = Cap_Austria.get()
为了使它不仅有一个默认文本,但是如果你点击它就会消失,如果你没有输入任何东西就点击它就会回来。祝你好运!