为什么在 python 中使用 bind with button 会出现 AttributeError
Why is there an AttributeError using bind with button in python
我想在鼠标悬停在按钮上方时立即更改按钮的颜色,我知道,但我遇到了错误
我正在使用 tkinter python 的绑定函数来创建悬停效果,我搜索了 Whosebug 并发现了 and 的成功实现,我试图将其实现到我的代码中,但到目前为止我没有成功。我查看了这段代码:
def clicked():
res = "Welcome to " + txt.get()
lbl.configure(text= res)
def on_enter(e):
btn['background'] = '#FFD51B'
def on_leave(e):
btn['background'] = '#0A22B1'
root = Tk()
root.geometry("1366x768")
root.configure(bg="blue")
#root.attributes('-fullscreen', True)
root.state('zoomed')
abc=Frame(root,bg="blue",height=200,width=100)
abc.pack(side='top')
abc1=Frame(root,bg="blue",height=400,width=510)
abc1.pack(side='top')
a = Label(abc1 ,text = "First Name:",fg="white",
font="Courier 20 bold",bg="blue").place(x=1,y=1)
b = Label(abc1 ,text = "Last Name:",fg="white",
font="Courier 20 bold",bg="blue").place(x=1,y=42)
c = Label(abc1 ,text = "Email Id:",fg="white",
font="Courier 20 bold",bg="blue").place(x=1,y=83)
d = Label(abc1 ,text = "Contact Number:",fg="white",
font="Courier 20 bold",bg="blue").place(x=1,y=124)
a1 = Entry(abc1,width=25).place(x=300,y=9)
b1 = Entry(abc1,width=25).place(x=300,y=51)
c1 = Entry(abc1,width=25).place(x=300,y=92)
d1 = Entry(abc1,width=25).place(x=300,y=134)
btn = Button(abc1 ,text="Submit",height=2,width=20,activebackground="#FFD51B",
bg="#0A22B1",fg="white",font="Courier 11 bold").place(x=150,y=176)
btn.bind("<Enter>", on_enter)
btn.bind("<Leave>", on_leave)
root.mainloop()
错误是:-
btn.bind("", on_enter)
AttributeError: 'NoneType' 对象没有属性 'bind'
我不知道我做错了什么,据我所知代码在逻辑上是正确的
您不是将变量 btn
绑定到按钮对象,而是绑定到调用按钮的 place
方法的结果,即 None - 因此 btn
是 None.
走这条线:
btn = Button(abc1 ,text="Submit",height=2,width=20,activebackground="#FFD51B",
bg="#0A22B1",fg="white",font="Courier 11 bold").place(x=150,y=176)
并将其分成两行:
btn = Button(abc1 ,text="Submit",height=2,width=20,activebackground="#FFD51B",
bg="#0A22B1",fg="white",font="Courier 11 bold")
btn.place(x=150,y=176)
我想在鼠标悬停在按钮上方时立即更改按钮的颜色,我知道,但我遇到了错误
我正在使用 tkinter python 的绑定函数来创建悬停效果,我搜索了 Whosebug 并发现了 and 的成功实现,我试图将其实现到我的代码中,但到目前为止我没有成功。我查看了这段代码:
def clicked():
res = "Welcome to " + txt.get()
lbl.configure(text= res)
def on_enter(e):
btn['background'] = '#FFD51B'
def on_leave(e):
btn['background'] = '#0A22B1'
root = Tk()
root.geometry("1366x768")
root.configure(bg="blue")
#root.attributes('-fullscreen', True)
root.state('zoomed')
abc=Frame(root,bg="blue",height=200,width=100)
abc.pack(side='top')
abc1=Frame(root,bg="blue",height=400,width=510)
abc1.pack(side='top')
a = Label(abc1 ,text = "First Name:",fg="white",
font="Courier 20 bold",bg="blue").place(x=1,y=1)
b = Label(abc1 ,text = "Last Name:",fg="white",
font="Courier 20 bold",bg="blue").place(x=1,y=42)
c = Label(abc1 ,text = "Email Id:",fg="white",
font="Courier 20 bold",bg="blue").place(x=1,y=83)
d = Label(abc1 ,text = "Contact Number:",fg="white",
font="Courier 20 bold",bg="blue").place(x=1,y=124)
a1 = Entry(abc1,width=25).place(x=300,y=9)
b1 = Entry(abc1,width=25).place(x=300,y=51)
c1 = Entry(abc1,width=25).place(x=300,y=92)
d1 = Entry(abc1,width=25).place(x=300,y=134)
btn = Button(abc1 ,text="Submit",height=2,width=20,activebackground="#FFD51B",
bg="#0A22B1",fg="white",font="Courier 11 bold").place(x=150,y=176)
btn.bind("<Enter>", on_enter)
btn.bind("<Leave>", on_leave)
root.mainloop()
错误是:- btn.bind("", on_enter) AttributeError: 'NoneType' 对象没有属性 'bind' 我不知道我做错了什么,据我所知代码在逻辑上是正确的
您不是将变量 btn
绑定到按钮对象,而是绑定到调用按钮的 place
方法的结果,即 None - 因此 btn
是 None.
走这条线:
btn = Button(abc1 ,text="Submit",height=2,width=20,activebackground="#FFD51B",
bg="#0A22B1",fg="white",font="Courier 11 bold").place(x=150,y=176)
并将其分成两行:
btn = Button(abc1 ,text="Submit",height=2,width=20,activebackground="#FFD51B",
bg="#0A22B1",fg="white",font="Courier 11 bold")
btn.place(x=150,y=176)