2017-07-30 39 views
0

我想做一個簡單的用戶輸入tkinter GUI,用戶可以在其中輸入基本的細節。雖然我在網上搜索,我不能征服這個問題:Python - 入口沒有屬性小部件

from tkinter import * 


def user(): 
    print(user_entry.widget.get()) 
    print(email_entry.widget.get()) 
    print(pass1_entry.widget.get()) 
    print(pass2_entry.widget.get()) 
    pass 

#main loop initial visual 
main_window = Tk() 
main_window.title('Register') 
main_window.geometry('250x350+200+200') 
main_window.configure(bg = 'Blue') 

wel = Label(main_window, text="""Hello, thanks for using my software. 
Please put in the details, it will be kept 
safe and sent to our database""", bg = 'white') 
wel.place(x = 0,y = 10) 


username_lab = Label(main_window, text="Username:").place(x=0, y=64) 

#us = StringVar() 
user_entry = Entry(main_window) 
user_entry.place(x=70,y=64) 
user_entry.bind("<Return>", user()) 

email_lab = Label(main_window, text="Email: ").place(x=0, y=90) 

#emi = StringVar() 
email_entry = Entry(main_window) 
email_entry.place(x=70,y=90) 
email_entry.bind("<Return>", user()) 

pass_lab = Label(main_window, text="Password:").place(x=0, y=116) 

#pas1 = StringVar() 
pass1_entry = Entry(main_window, show="*") 
pass1_entry.place(x=70,y=116) 
pass1_entry.bind("<Return>", user()) 

pass_lab = Label(main_window, text="Password repeat:").place(x=0, y=140) 

#pas2 = StringVar() 
pass2_entry = Entry(main_window, show="*") 
pass2_entry.place(x=100,y=140) 
pass2_entry.bind("<Return>", user()) 


ok = Button(main_window, text="OK", command = user(), width=8).place(x=15, y= 175) 




main_window.mainloop() 

不幸的是,我不斷收到這個錯誤,我想不通爲什麼。請幫助至少一件事(即用戶名),其餘的將遵循類似的規則。 :

Traceback (most recent call last): 
    File "C:/Users/Milosz/Desktop/Programming/Register/main.py", line 28, in <module> 
    user_entry.bind("<Return>", user()) 
    File "C:/Users/Milosz/Desktop/Programming/Register/main.py", line 5, in user 
    print(user_entry.widget.get()) 
AttributeError: 'Entry' object has no attribute 'widget' 
+2

你爲什麼認爲'user_entry'具有'widget'屬性?你期望這個屬性與'user_entry'對象本身有什麼不同? –

+0

我不太確定。那麼,如何編寫代碼以便在SHELL上打印輸入?謝謝。 –

回答

0

當你需要在你需要在這種情況下,只有使用它的變量名的輸入字段使用get()widget()沒有做你認爲它在這裏做什麼,也不需要。

在您的user():類中替換所有的打印語句。

來源:

def user(): 
    print(user_entry.widget.get()) 
    print(email_entry.widget.get()) 
    print(pass1_entry.widget.get()) 
    print(pass2_entry.widget.get()) 
    pass 

要:

def user(): 
    print(user_entry.get()) 
    print(email_entry.get()) 
    print(pass1_entry.get()) 
    print(pass2_entry.get()) 
    # removed pass as it is not needed and does nothing here. 

這應該打印內容到控制檯。

相關問題