如何将登录注册GUI代码拆分为面向对象的Loginform.py与Registrationform.py
面向对象重构登录注册GUI代码
需求说明
将原有登录注册GUI代码拆分为Loginform.py、Registrationform.py和主文件main.py,通过面向对象方式重构,简化代码结构、提升整洁度。
重构后代码实现
1. 主文件 main.py
负责创建主窗口、加载两个表单组件并实现页面切换逻辑:
from tkinter import Tk, PhotoImage from Loginform import LoginForm from Registrationform import RegistrationForm if __name__ == "__main__": window = Tk() window.rowconfigure(0, weight=1) window.columnconfigure(0, weight=1) window.state('zoomed') window.resizable(0, 0) window.title('Login and Registration Page') # 设置窗口图标 icon = PhotoImage(file='images\\pic-icon.png') window.iconphoto(True, icon) # 实例化登录和注册表单 login_frame = LoginForm(window) registration_frame = RegistrationForm(window) # 布局两个表单 for frame in (login_frame, registration_frame): frame.grid(row=0, column=0, sticky='nsew') # 页面切换函数 def show_frame(frame): frame.tkraise() # 初始显示登录页 show_frame(login_frame) # 给两个表单传递页面切换函数,实现互相跳转 login_frame.set_switch_func(lambda: show_frame(registration_frame)) registration_frame.set_switch_func(lambda: show_frame(login_frame)) window.mainloop()
2. Loginform.py - 登录表单类
封装登录页面的所有控件和逻辑:
from tkinter import Frame, Listbox, Entry, Button, StringVar, messagebox class LoginForm(Frame): def __init__(self, parent): super().__init__(parent) self.switch_func = None self._create_widgets() def _create_widgets(self): # 左侧设计区域 design_frame1 = Listbox(self, bg='#0c71b9', width=250, height=100, highlightthickness=0, borderwidth=0) design_frame1.place(x=0, y=0) # 邮箱输入框 self.Email = StringVar() # 注:原代码中design_frame4未定义,此处直接使用当前表单容器,可根据实际布局调整 email_entry = Entry(self, fg="#a7a7a7", font=("yu gothic ui semibold", 12), highlightthickness=2, textvariable=self.Email) email_entry.place(x=500, y=200) # 替换为实际位置坐标 # 密码输入框 self.Password = StringVar() self.password_entry1 = Entry(self, fg="#a7a7a7", font=("yu gothic ui semibold", 12), highlightthickness=2, textvariable=self.Password, show='•') self.password_entry1.place(x=500, y=260) # 替换为实际位置坐标 # 显示/隐藏密码按钮 password_toggle_btn = Button(self, text="显示密码", command=self.password_command) password_toggle_btn.place(x=700, y=260) # 替换为实际位置坐标 # 注册跳转按钮 register_btn = Button(self, text="没有账号?立即注册", command=self._switch_to_registration) register_btn.place(x=500, y=320) # 替换为实际位置坐标 # 登录按钮 login_btn = Button(self, text="登录", command=self._handle_login) login_btn.place(x=500, y=380) # 替换为实际位置坐标 def password_command(self): """切换密码显示/隐藏状态""" if self.password_entry1.cget('show') == '•': self.password_entry1.config(show='') else: self.password_entry1.config(show='•') def _handle_login(self): """处理登录逻辑,可补充sqlite3验证逻辑""" email = self.Email.get() password = self.Password.get() if not email or not password: messagebox.showerror("错误", "请填写完整账号信息") return # 此处添加数据库验证逻辑(如sqlite3查询) def _switch_to_registration(self): """跳转到注册页面""" if self.switch_func: self.switch_func() def set_switch_func(self, func): """设置页面切换函数""" self.switch_func = func
3. Registrationform.py - 注册表单类
封装注册页面的控件和逻辑:
from tkinter import Frame, Entry, Button, StringVar, messagebox import sqlite3 class RegistrationForm(Frame): def __init__(self, parent): super().__init__(parent) self.switch_func = None self._create_widgets() def _create_widgets(self): # 注册页面布局,可参考登录页结构调整样式 # 用户名输入框 self.Username = StringVar() username_entry = Entry(self, fg="#a7a7a7", font=("yu gothic ui semibold", 12), highlightthickness=2, textvariable=self.Username) username_entry.place(x=500, y=180) # 替换为实际位置坐标 # 邮箱输入框 self.Email = StringVar() email_entry = Entry(self, fg="#a7a7a7", font=("yu gothic ui semibold", 12), highlightthickness=2, textvariable=self.Email) email_entry.place(x=500, y=240) # 替换为实际位置坐标 # 密码输入框 self.Password = StringVar() password_entry = Entry(self, fg="#a7a7a7", font=("yu gothic ui semibold", 12), highlightthickness=2, textvariable=self.Password, show='•') password_entry.place(x=500, y=300) # 替换为实际位置坐标 # 登录跳转按钮 login_btn = Button(self, text="已有账号?立即登录", command=self._switch_to_login) login_btn.place(x=500, y=360) # 替换为实际位置坐标 # 注册按钮 register_btn = Button(self, text="注册", command=self._handle_registration) register_btn.place(x=500, y=420) # 替换为实际位置坐标 def _handle_registration(self): """处理注册逻辑,添加sqlite3存储逻辑""" username = self.Username.get() email = self.Email.get() password = self.Password.get() if not username or not email or not password: messagebox.showerror("错误", "请填写完整注册信息") return # 数据库存储逻辑 conn = sqlite3.connect('user.db') c = conn.cursor() # 初始化用户表(首次运行时创建) c.execute('''CREATE TABLE IF NOT EXISTS users (username TEXT, email TEXT, password TEXT)''') # 插入用户数据 c.execute("INSERT INTO users VALUES (?, ?, ?)", (username, email, password)) conn.commit() conn.close() messagebox.showinfo("成功", "注册完成") def _switch_to_login(self): """跳转到登录页面""" if self.switch_func: self.switch_func() def set_switch_func(self, func): """设置页面切换函数""" self.switch_func = func
重构要点说明
- 面向对象封装:将登录、注册页面各自封装为独立的
Frame子类,控件初始化、业务逻辑内聚在类中,职责清晰。 - 代码拆分:三个文件各司其职,主文件负责窗口管理和页面切换,表单文件专注自身UI与逻辑。
- 解耦设计:通过传递页面切换函数实现表单跳转,避免直接依赖主窗口,提升可维护性。
- 可扩展性:后续修改登录/注册逻辑时,仅需修改对应文件,不影响其他代码模块。
内容的提问来源于stack exchange,提问作者cj_123
相关产品推荐
相关产品推荐

