【问题标题】:Python can't assign function to callPython无法分配函数来调用
【发布时间】:2015-02-05 15:03:44
【问题描述】:

我正在 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()

【问题讨论】:

    标签: python function tkinter


    【解决方案1】:

    您不能在赋值语句的左侧调用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())

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-01
      • 2021-06-06
      • 2015-04-23
      • 1970-01-01
      • 2013-03-01
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多