2014-06-29 74 views
0

我有一個文本框創建,但我怎麼會得到一個函數的輸入。閱讀文本框的內容

userbox = Text(root, width = 10, height = 1, wrap = WORD) 
userbox.pack() 

passwordbox = Text(root, width = 10, height = 1, wrap = WORD) 
passwordbox.pack() 

b2 = Button(root,text="Submit Login") 
b2.pack() 
b2.configure(command=login) 

我如何得到這個被處理。 I.E.如果用戶名=「名」和密碼=「密碼」 print "welcome"如果不是print"incorrect login"

+0

你真的應該使用['Entry'部件(http://effbot.org/tkinterbook/entry.htm)。 –

回答

2

由於@BurhanKhalid指出,你應該使用Tk.Entry()插件這樣的事情。假設您使用的是Tk.Entry小部件,則可以使用get()屬性獲取小部件的文本。

代碼:

def login(): 
    if userbox.get() == "name" and passwordbox.get() == "password": 
     print "welcome" 
    else: 
     print"incorrect login" 

userbox = Entry(root, width = 10) 
userbox.pack() 

passwordbox = Entry(root, width = 10) 
passwordbox.pack() 

b2 = Button(root,text="Submit Login") 
b2.pack() 
b2.configure(command=login)