鱼C论坛

 找回密码
 立即注册
查看: 68|回复: 3

[已解决]AttributeError: 'LoginPage' object has no attribute 'bottonLogin'

[复制链接]
发表于 2024-5-15 21:35:30 | 显示全部楼层 |阅读模式
10鱼币
  1. '''
  2. import tkinter as tk

  3. window = tk.Tk()

  4. screen_width = window.winfo_screenwidth()
  5. screen_height = window.winfo_screenheight()

  6. x=(screen_width-200)/2
  7. y=(screen_height-100)/2

  8. window.geometry('220x100+%d+%d'%(x,y))
  9. window.title('Login')

  10. #在左侧放置标签显示logo图片
  11. logo = tk.PhotoImage(file = "logo.png")
  12. tk.Label(window,justify = tk.LEFT,image = logo).grid(row = 0, column = 0,rowspan=2)
  13. tk.Label(window,text='账号',font=('微软雅黑',12)).grid(row=0,column=1)
  14. tk.Label(window,text='密码',font=('微软雅黑',12)).grid(row=1,column=1)


  15. window.mainloop()

  16. '''

  17. from tkinter import  *

  18. FONT = ('Microsoft YaHei',12)

  19. #定义页面类
  20. class LoginPage():
  21.     def __init__(self,master):
  22.         self.master = master
  23.         self.initUI()

  24.     def initUI(self):
  25.         #初始化UI
  26.         self.centerWindow()
  27.         #图片文件
  28.         self.logo = PhotoImage(file="logo.png")
  29.         self.iconLogin = PhotoImage(file="iconLogin.png")
  30.         self.iconCancel = PhotoImage(file="iconCancel.png")
  31.         print(self.logo)
  32.         Label(self.master,justify=LEFT,image=self.logo).grid(row=0,column=0,rowspan=2)
  33.         Label(self.master,text='账号',font=FONT).grid(row=0,column=1)
  34.         Label(self.master,text='密码',font=FONT).grid(row=1, column=1)
  35.         #在标签的右侧放置输入控件
  36.         self.entryUser = Entry(self.master,width=15)
  37.         self.entryUser.grid(row=0,column=2)
  38.         self.entryPsw = Entry(self.master,width=15,show='*')
  39.         self.entryPsw.grid(row=1,column=2)
  40.         #放置2个按钮
  41.         Button(self.master,compound=LEFT,image=self.iconLogin,text='登录',font=FONT,
  42.                 command=self.bottonLogin).grid(row=2,column=1)
  43.         Button(self.master, compound=LEFT, image=self.iconCancel, text='取消', font=FONT,
  44.                command=self.master.quit).grid(row=2, column=2)

  45.     def buttonLogin(self):
  46.         user = self.entryUser.get()
  47.         psw = self.entryPsw.get()
  48.         if user and  psw:
  49.             print("UserName:%s\n Password:%s"%(user,psw))
  50.         else:
  51.             print("Please Enter UserName and Password!")

  52.     def centerWindow(self):
  53.         screen_width = self.master.winfo_screenwidth()
  54.         screen_height = self.master.winfo_screenheight()
  55.         x=(screen_width - 200) / 2
  56.         y = (screen_height - 100) / 2
  57.         self.master.geometry('260x100+%d+%d'%(x,y))
  58.         self.master.title("LoginGUI")
  59.         pass


  60. def main():
  61.     window = Tk()
  62.     LoginPage(window)

  63.     window.mainloop()


  64. if __name__ == '__main__':
  65.      main()




复制代码


求助各位大佬!!!

  1. Traceback (most recent call last):
  2.   File "D:/pythoncode/assignment5/Scripts/src/fangloge.py", line 84, in <module>
  3.     main()
  4.   File "D:/pythoncode/assignment5/Scripts/src/fangloge.py", line 78, in main
  5.     LoginPage(window)
  6.   File "D:/pythoncode/assignment5/Scripts/src/fangloge.py", line 34, in __init__
  7.     self.initUI()
  8.   File "D:/pythoncode/assignment5/Scripts/src/fangloge.py", line 54, in initUI
  9.     command=self.bottonLogin).grid(row=2,column=1)
  10. AttributeError: 'LoginPage' object has no attribute 'bottonLogin'
复制代码
最佳答案
2024-5-15 21:35:31
辞树 发表于 2024-05-15 21:36
最上面都是注释,不用看,是我上一个作业里的东西


非常抱歉,我误解了问题。根据你提供的错误信息和代码,发现问题出在你的代码中的拼写错误上。在 `LoginPage` 类中,你定义了一个名为 `buttonLogin` 的方法。但是,在创建按钮时,你却将该方法命名为 `bottonLogin`(缺少一个"t")。所以,只需将按钮的 `command` 参数中的方法名由 `bottonLogin` 改为 `buttonLogin` 即可。

修改后的代码如下:

  1. from tkinter import *

  2. FONT = ('Microsoft YaHei', 12)


  3. class LoginPage():
  4.     def __init__(self, master):
  5.         self.master = master
  6.         self.initUI()

  7.     def initUI(self):
  8.         self.centerWindow()
  9.         self.logo = PhotoImage(file="logo.png")
  10.         self.iconLogin = PhotoImage(file="iconLogin.png")
  11.         self.iconCancel = PhotoImage(file="iconCancel.png")
  12.         Label(self.master, justify=LEFT, image=self.logo).grid(row=0, column=0, rowspan=2)
  13.         Label(self.master, text='账号', font=FONT).grid(row=0, column=1)
  14.         Label(self.master, text='密码', font=FONT).grid(row=1, column=1)
  15.         self.entryUser = Entry(self.master, width=15)
  16.         self.entryUser.grid(row=0, column=2)
  17.         self.entryPsw = Entry(self.master, width=15, show='*')
  18.         self.entryPsw.grid(row=1, column=2)
  19.         Button(self.master, compound=LEFT, image=self.iconLogin, text='登录', font=FONT,
  20.                command=self.buttonLogin).grid(row=2, column=1)
  21.         Button(self.master, compound=LEFT, image=self.iconCancel, text='取消', font=FONT,
  22.                command=self.master.quit).grid(row=2, column=2)

  23.     def buttonLogin(self):
  24.         user = self.entryUser.get()
  25.         psw = self.entryPsw.get()
  26.         if user and psw:
  27.             print("UserName:%s\nPassword:%s" % (user, psw))
  28.         else:
  29.             print("Please Enter UserName and Password!")

  30.     def centerWindow(self):
  31.         screen_width = self.master.winfo_screenwidth()
  32.         screen_height = self.master.winfo_screenheight()
  33.         x = (screen_width - 200) / 2
  34.         y = (screen_height - 100) / 2
  35.         self.master.geometry('260x100+%d+%d' % (x, y))
  36.         self.master.title("LoginGUI")


  37. def main():
  38.     window = Tk()
  39.     LoginPage(window)
  40.     window.mainloop()


  41. if __name__ == '__main__':
  42.     main()
复制代码


希望能帮到你!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
logo.png
iconCancel.png
iconLogin.png

最佳答案

查看完整内容

非常抱歉,我误解了问题。根据你提供的错误信息和代码,发现问题出在你的代码中的拼写错误上。在 `LoginPage` 类中,你定义了一个名为 `buttonLogin` 的方法。但是,在创建按钮时,你却将该方法命名为 `bottonLogin`(缺少一个"t")。所以,只需将按钮的 `command` 参数中的方法名由 `bottonLogin` 改为 `buttonLogin` 即可。 修改后的代码如下: 希望能帮到你!如果还有其他问题,请随时提问。 以上回复来自 Fis ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-5-15 21:35:31 | 显示全部楼层    本楼为最佳答案   
辞树 发表于 2024-05-15 21:36
最上面都是注释,不用看,是我上一个作业里的东西


非常抱歉,我误解了问题。根据你提供的错误信息和代码,发现问题出在你的代码中的拼写错误上。在 `LoginPage` 类中,你定义了一个名为 `buttonLogin` 的方法。但是,在创建按钮时,你却将该方法命名为 `bottonLogin`(缺少一个"t")。所以,只需将按钮的 `command` 参数中的方法名由 `bottonLogin` 改为 `buttonLogin` 即可。

修改后的代码如下:

  1. from tkinter import *

  2. FONT = ('Microsoft YaHei', 12)


  3. class LoginPage():
  4.     def __init__(self, master):
  5.         self.master = master
  6.         self.initUI()

  7.     def initUI(self):
  8.         self.centerWindow()
  9.         self.logo = PhotoImage(file="logo.png")
  10.         self.iconLogin = PhotoImage(file="iconLogin.png")
  11.         self.iconCancel = PhotoImage(file="iconCancel.png")
  12.         Label(self.master, justify=LEFT, image=self.logo).grid(row=0, column=0, rowspan=2)
  13.         Label(self.master, text='账号', font=FONT).grid(row=0, column=1)
  14.         Label(self.master, text='密码', font=FONT).grid(row=1, column=1)
  15.         self.entryUser = Entry(self.master, width=15)
  16.         self.entryUser.grid(row=0, column=2)
  17.         self.entryPsw = Entry(self.master, width=15, show='*')
  18.         self.entryPsw.grid(row=1, column=2)
  19.         Button(self.master, compound=LEFT, image=self.iconLogin, text='登录', font=FONT,
  20.                command=self.buttonLogin).grid(row=2, column=1)
  21.         Button(self.master, compound=LEFT, image=self.iconCancel, text='取消', font=FONT,
  22.                command=self.master.quit).grid(row=2, column=2)

  23.     def buttonLogin(self):
  24.         user = self.entryUser.get()
  25.         psw = self.entryPsw.get()
  26.         if user and psw:
  27.             print("UserName:%s\nPassword:%s" % (user, psw))
  28.         else:
  29.             print("Please Enter UserName and Password!")

  30.     def centerWindow(self):
  31.         screen_width = self.master.winfo_screenwidth()
  32.         screen_height = self.master.winfo_screenheight()
  33.         x = (screen_width - 200) / 2
  34.         y = (screen_height - 100) / 2
  35.         self.master.geometry('260x100+%d+%d' % (x, y))
  36.         self.master.title("LoginGUI")


  37. def main():
  38.     window = Tk()
  39.     LoginPage(window)
  40.     window.mainloop()


  41. if __name__ == '__main__':
  42.     main()
复制代码


希望能帮到你!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-5-15 21:36:01 | 显示全部楼层
你好!从错误信息来看,问题出在 `LoginPage` 类中的 `bottonLogin` 方法上,因为拼写错误导致无法找到该方法。请将 `self.bottonLogin` 改为 `self.buttonLogin`,即将 `button` 第一个字母的大小写调整正确。修改后的代码如下:

  1. from tkinter import *

  2. FONT = ('Microsoft YaHei', 12)


  3. class LoginPage():
  4.     def __init__(self, master):
  5.         self.master = master
  6.         self.initUI()

  7.     def initUI(self):
  8.         self.centerWindow()
  9.         self.logo = PhotoImage(file="logo.png")
  10.         self.iconLogin = PhotoImage(file="iconLogin.png")
  11.         self.iconCancel = PhotoImage(file="iconCancel.png")
  12.         Label(self.master, justify=LEFT, image=self.logo).grid(row=0, column=0, rowspan=2)
  13.         Label(self.master, text='账号', font=FONT).grid(row=0, column=1)
  14.         Label(self.master, text='密码', font=FONT).grid(row=1, column=1)
  15.         self.entryUser = Entry(self.master, width=15)
  16.         self.entryUser.grid(row=0, column=2)
  17.         self.entryPsw = Entry(self.master, width=15, show='*')
  18.         self.entryPsw.grid(row=1, column=2)
  19.         Button(self.master, compound=LEFT, image=self.iconLogin, text='登录', font=FONT,
  20.                command=self.buttonLogin).grid(row=2, column=1)
  21.         Button(self.master, compound=LEFT, image=self.iconCancel, text='取消', font=FONT,
  22.                command=self.master.quit).grid(row=2, column=2)

  23.     def buttonLogin(self):
  24.         user = self.entryUser.get()
  25.         psw = self.entryPsw.get()
  26.         if user and psw:
  27.             print("UserName:%s\nPassword:%s" % (user, psw))
  28.         else:
  29.             print("Please Enter UserName and Password!")

  30.     def centerWindow(self):
  31.         screen_width = self.master.winfo_screenwidth()
  32.         screen_height = self.master.winfo_screenheight()
  33.         x = (screen_width - 200) / 2
  34.         y = (screen_height - 100) / 2
  35.         self.master.geometry('260x100+%d+%d' % (x, y))
  36.         self.master.title("LoginGUI")


  37. def main():
  38.     window = Tk()
  39.     LoginPage(window)
  40.     window.mainloop()


  41. if __name__ == '__main__':
  42.     main()
复制代码


希望能帮到你!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-5-15 21:36:40 | 显示全部楼层
最上面都是注释,不用看,是我上一个作业里的东西
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-29 17:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表