|
10鱼币
- '''
- import tkinter as tk
- window = tk.Tk()
- screen_width = window.winfo_screenwidth()
- screen_height = window.winfo_screenheight()
- x=(screen_width-200)/2
- y=(screen_height-100)/2
- window.geometry('220x100+%d+%d'%(x,y))
- window.title('Login')
- #在左侧放置标签显示logo图片
- logo = tk.PhotoImage(file = "logo.png")
- tk.Label(window,justify = tk.LEFT,image = logo).grid(row = 0, column = 0,rowspan=2)
- tk.Label(window,text='账号',font=('微软雅黑',12)).grid(row=0,column=1)
- tk.Label(window,text='密码',font=('微软雅黑',12)).grid(row=1,column=1)
- window.mainloop()
- '''
- from tkinter import *
- FONT = ('Microsoft YaHei',12)
- #定义页面类
- class LoginPage():
- def __init__(self,master):
- self.master = master
- self.initUI()
- def initUI(self):
- #初始化UI
- self.centerWindow()
- #图片文件
- self.logo = PhotoImage(file="logo.png")
- self.iconLogin = PhotoImage(file="iconLogin.png")
- self.iconCancel = PhotoImage(file="iconCancel.png")
- print(self.logo)
- Label(self.master,justify=LEFT,image=self.logo).grid(row=0,column=0,rowspan=2)
- Label(self.master,text='账号',font=FONT).grid(row=0,column=1)
- Label(self.master,text='密码',font=FONT).grid(row=1, column=1)
- #在标签的右侧放置输入控件
- self.entryUser = Entry(self.master,width=15)
- self.entryUser.grid(row=0,column=2)
- self.entryPsw = Entry(self.master,width=15,show='*')
- self.entryPsw.grid(row=1,column=2)
- #放置2个按钮
- Button(self.master,compound=LEFT,image=self.iconLogin,text='登录',font=FONT,
- command=self.bottonLogin).grid(row=2,column=1)
- Button(self.master, compound=LEFT, image=self.iconCancel, text='取消', font=FONT,
- command=self.master.quit).grid(row=2, column=2)
- def buttonLogin(self):
- user = self.entryUser.get()
- psw = self.entryPsw.get()
- if user and psw:
- print("UserName:%s\n Password:%s"%(user,psw))
- else:
- print("Please Enter UserName and Password!")
- def centerWindow(self):
- screen_width = self.master.winfo_screenwidth()
- screen_height = self.master.winfo_screenheight()
- x=(screen_width - 200) / 2
- y = (screen_height - 100) / 2
- self.master.geometry('260x100+%d+%d'%(x,y))
- self.master.title("LoginGUI")
- pass
- def main():
- window = Tk()
- LoginPage(window)
- window.mainloop()
- if __name__ == '__main__':
- main()
复制代码
求助各位大佬!!!
- Traceback (most recent call last):
- File "D:/pythoncode/assignment5/Scripts/src/fangloge.py", line 84, in <module>
- main()
- File "D:/pythoncode/assignment5/Scripts/src/fangloge.py", line 78, in main
- LoginPage(window)
- File "D:/pythoncode/assignment5/Scripts/src/fangloge.py", line 34, in __init__
- self.initUI()
- File "D:/pythoncode/assignment5/Scripts/src/fangloge.py", line 54, in initUI
- command=self.bottonLogin).grid(row=2,column=1)
- AttributeError: 'LoginPage' object has no attribute 'bottonLogin'
复制代码
非常抱歉,我误解了问题。根据你提供的错误信息和代码,发现问题出在你的代码中的拼写错误上。在 `LoginPage` 类中,你定义了一个名为 `buttonLogin` 的方法。但是,在创建按钮时,你却将该方法命名为 `bottonLogin`(缺少一个"t")。所以,只需将按钮的 `command` 参数中的方法名由 `bottonLogin` 改为 `buttonLogin` 即可。
修改后的代码如下:
- from tkinter import *
- FONT = ('Microsoft YaHei', 12)
- class LoginPage():
- def __init__(self, master):
- self.master = master
- self.initUI()
- def initUI(self):
- self.centerWindow()
- self.logo = PhotoImage(file="logo.png")
- self.iconLogin = PhotoImage(file="iconLogin.png")
- self.iconCancel = PhotoImage(file="iconCancel.png")
- Label(self.master, justify=LEFT, image=self.logo).grid(row=0, column=0, rowspan=2)
- Label(self.master, text='账号', font=FONT).grid(row=0, column=1)
- Label(self.master, text='密码', font=FONT).grid(row=1, column=1)
- self.entryUser = Entry(self.master, width=15)
- self.entryUser.grid(row=0, column=2)
- self.entryPsw = Entry(self.master, width=15, show='*')
- self.entryPsw.grid(row=1, column=2)
- Button(self.master, compound=LEFT, image=self.iconLogin, text='登录', font=FONT,
- command=self.buttonLogin).grid(row=2, column=1)
- Button(self.master, compound=LEFT, image=self.iconCancel, text='取消', font=FONT,
- command=self.master.quit).grid(row=2, column=2)
- def buttonLogin(self):
- user = self.entryUser.get()
- psw = self.entryPsw.get()
- if user and psw:
- print("UserName:%s\nPassword:%s" % (user, psw))
- else:
- print("Please Enter UserName and Password!")
- def centerWindow(self):
- screen_width = self.master.winfo_screenwidth()
- screen_height = self.master.winfo_screenheight()
- x = (screen_width - 200) / 2
- y = (screen_height - 100) / 2
- self.master.geometry('260x100+%d+%d' % (x, y))
- self.master.title("LoginGUI")
- def main():
- window = Tk()
- LoginPage(window)
- window.mainloop()
- if __name__ == '__main__':
- main()
复制代码
希望能帮到你!如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
最佳答案
查看完整内容
非常抱歉,我误解了问题。根据你提供的错误信息和代码,发现问题出在你的代码中的拼写错误上。在 `LoginPage` 类中,你定义了一个名为 `buttonLogin` 的方法。但是,在创建按钮时,你却将该方法命名为 `bottonLogin`(缺少一个"t")。所以,只需将按钮的 `command` 参数中的方法名由 `bottonLogin` 改为 `buttonLogin` 即可。
修改后的代码如下:
希望能帮到你!如果还有其他问题,请随时提问。
以上回复来自 Fis ...
|