2015-02-05 85 views
0

我正在做一個python項目在tkinter中找出你的年齡。在一行中它說不能分配函數來調用。這裏是行:Python不能分配函數來調用

int(y_born_in) = Entry(window, width=20, bg="yellow", foreground = "purple",  font = "X-Files") 

(我覺得無聊,所以我加入的字體&東西)

這裏是我的實際代碼,如果你需要它:

from tkinter import * 
#key press function: 
def click(): 
    output.delete(0.0, END) 
    output.insert (2014 - int(y_born_in)) 
    output.insert ("\n or it could be") 
    output.insert (2015 - int(y_born_in)) 
#code for the enter button 
def enter_click(Event): 
    output.delete(0.0, END) 
    output.insert (2014 - int(y_born_in)) 
    output.insert ("\n or it could be") 
    output.insert (2015 - int(y_born_in)) 


#collect text from text entry box 
window = Tk() 
window.title("how old are you?") 
window.configure(background = "cyan") 
#create label: 
Label(window, text="please insert the year you were born", background = "hot pink",foreground = "light green", font = "X-Files") .grid(sticky=N) 
#create text entry box 
int(y_born_in) = Entry(window, width=20, bg="yellow", foreground = "purple", font = "X-Files") 
y_born_in.grid(row=1, column=0,sticky=N) 
#add a submit button 
submit = Button(window, text="SUBMIT", width=6, command=click, background = "purple", fg = "yellow", font = "X-Files") .grid(sticky=N) 

#create another label 
Label(window, text="you were born in the year", background = "blue", foreground = "red", font = "X-Files") .grid(sticky=N) 

#create text box 
output = Text(window, width=75, height=8, wrap=WORD, background="coral", fg = "blue", font = "X-Files") 
output.grid(sticky=N) 

#enter key works as the button 
window.bind("<Return>", enter_click) 


window.mainloop() 

回答

2

你不能叫int在轉讓聲明的左側。相反,只是做:

y_born_in = Entry(window, width=20, bg="yellow", foreground = "purple",  font = "X-Files") 

如果你想告訴程序,「y_born_in應該只接受數字輸入的條目」,這是一個有點棘手。詳情請參閱Interactively validating Entry widget content in tkinterAdding validation to an Entry widget


此外,無處不在,你試圖做int(y_born_in)來獲取用戶輸入的內容(如在clickenter_click),則應該做int(y_born_in.get())