我正在尝试从另一个名为 LoginOrReg 的文件运行一个名为 HabitTracker 的 python 文件,但是当我这样做时,它会重新运行 LoginOrReg 文件。
我已经尝试过这个
class LoginOrReg:
def __init__(self, db, cursor, login_username, data):
self.db = mysql.connector.connect(host = '**', user = '**', password= '**', database="**")
self.cursor = self.db.cursor()
self.login_username = login_username
self.data = data
# Asks user if they want to login or register
def login_or_reg(self, root):
self.root = customtkinter.CTk()
self.root.geometry('175x175')
self.login_button = customtkinter.CTkButton(self.root, text='Login', command=self.login).pack(pady=20)
self.reg_button = customtkinter.CTkButton(self.root, text='Register', command=self.reg_user).pack(pady=10)
self.root.mainloop()
def login(self):
self.login_window = customtkinter.CTk()
self.login_window.geometry('400x400')
self.login_username_entry = customtkinter.CTkEntry(self.login_window, placeholder_text ='Enter Your Username')
self.login_username_entry.pack(pady=30)
self.login_username = self.login_username_entry.get()
self.login_password_entry = customtkinter.CTkEntry(self.login_window, placeholder_text ='Enter Your Password')
self.login_password_entry.pack(pady=30)
self.login_password = self.login_password_entry.get()
self.log_user_in_button = customtkinter.CTkButton(self.login_window, text = 'Login', command = self.log_user_in)
self.log_user_in_button.pack(pady=30)
self.reg_button = customtkinter.CTkButton(self.login_window, text = 'Register New User', command = self.reg_user)
self.reg_button.pack(pady=20)
self.login_window.mainloop()
#Logs the user in
def log_user_in(self):
import HabitTracker as HT
self.habit = HT.HabitTracker(main_window=' ', sorting_options= ' ', sort= ' ', login= ' ')
self.cursor.execute('SELECT username AND password, COUNT(*) FROM users WHERE username=%s AND password=%s GROUP BY username',(self.login_username_entry.get(), self.login_password_entry.get()))
self.cursor.fetchall()
self.row_count = self.cursor.rowcount
if self.row_count == 1:
self.cursor.execute('SELECT userID FROM users WHERE username=%s AND password=%s',(self.login_username_entry.get(), self.login_password_entry.get()))
self.data = self.cursor.fetchone()
print(self.data)
for entry in self.data:
self.user_id = self.data[0]
self.habit.main_menu()
else:
self.my_label = customtkinter.CTkLabel(self.login_window, text = "User Not Found. Please Try Again.", font=('Helvetica', 10)).pack(pady=10)
login_reg = LoginOrReg(db = " ", cursor=" ", login_username= ' ', data = ' ')
login_reg.login_or_reg(root=" ")
如您所见,我已导入文件 Habit Tracker作为我的“log_user_in”方法中的 HT。 当我单击登录功能中规定的登录按钮时,我认为它甚至不运行 Log_user_in 函数,因为我放在那里的打印语句没有显示在终端中。我的目标是运行 HabitTracker 文件中的 Main_menu 方法。
这是我在主文件中导入 LoginOrReg 文件和类的位置:
class HabitTracker:
def __init__(self, main_window, sorting_options, sort, login) -> None:
self.main_window = main_window
self.db = mysql.connector.connect(host = '***', user = '**', password= '**', database="**")
self.cursor = self.db.cursor()
self.sorting_options = sorting_options
self.sort = sort
self.login = login
def main_menu(self):
import LoginOrReg as LR
self.login = LR.LoginOrReg(db=' ', cursor= ' ', login_username=' ', data= ' ')
self.date = datetime.today()
self.main_window = customtkinter.CTk()
self.main_window.geometry('1080x720')
self.sorting_options = ['Daily', 'Weekly', 'Monthly', 'Highest Streak',]
self.mylabel = customtkinter.CTkLabel(self.main_window, text='Welcome {}'.format(self.login.login_username), font=('Arial', 40)).pack(pady=5)
self.sort = customtkinter.CTkComboBox(self.main_window, values= self.sorting_options, command= sort_by.sort_by)
self.sort.set('Sort By')
self.sort.pack(pady=10)
self.create_new_habit_button = customtkinter.CTkButton(self.main_window, text='Create New Habit', command=create_habit.create_habit, fg_color='green').pack(pady=20)
self.cursor.execute('SELECT habit_name FROM habit WHERE user_id=%s GROUP BY habit_name', (self.login.data))
self.habits = self.cursor.fetchall()
self.j = 0
for j in self.habits:
self.habit_button = customtkinter.CTkButton(self.main_window, text=self.habits[self.j], command=lambda k = self.habits[self.j]: edit_habit.edit_habit(k))
self.habit_button.pack(pady=10)
self.j = self.j + 1
self.main_window.mainloop()
habit_tracker = HabitTracker(main_window= ' ', sorting_options= ' ', sort = " ", login=' ')
该问题源于在事件循环仍在运行时创建新的顶级窗口(使用
Tk()
或
CTk()
)。这会导致不可预测的行为,并且会中断预期的程序流程。
以下是如何解决此问题的分解方法,以及对代码进行更清晰、更易于管理的调整:
HabitTracker.py
import customtkinter
import mysql.connector
from datetime import datetime
class HabitTracker:
def __init__(self, main_window, user_id):
self.main_window = main_window
self.user_id = user_id
self.db = mysql.connector.connect(host='***', user='**', password='**', database="**")
self.cursor = self.db.cursor()
def main_menu(self):
self.main_window.destroy() # 关闭登录窗口
self.main_window = customtkinter.CTk()
self.main_window.geometry('1080x720')
self.main_window.title("Habit Tracker")
self.date = datetime.today()
customtkinter.CTkLabel(self.main_window, text=f'欢迎 {self.user_id}', font=('Arial', 40)).pack(pady=5)
sorting_options = ['Daily', 'Weekly', 'Monthly', 'Highest Streak']
self.sort = customtkinter.CTkComboBox(self.main_window, values=sorting_options)
self.sort.set('排序方式')
self.sort.pack(pady=10)
customtkinter.CTkButton(self.main_window, text='创建新习惯', command=self.create_habit, fg_color='green').pack(pady=20)
self.cursor.execute('SELECT habit_name FROM habit WHERE user_id=%s GROUP BY habit_name', (self.user_id,))
habits = self.cursor.fetchall()
for j, habit in enumerate(habits):
customtkinter.CTkButton(self.main_window, text=habit[0], command=lambda h=habit[0]: self.edit_habit(h)).pack(pady=10)
self.main_window.mainloop()
def create_habit(self):
# 在此处添加创建习惯的逻辑
pass
def edit_habit(self, habit_name):
# 在此处添加编辑习惯的逻辑
pass
# 我们将在登录成功后创建 HabitTracker 实例
LoginOrReg.py
import customtkinter
import mysql.connector
from HabitTracker import HabitTracker
class LoginOrReg:
def __init__(self):
self.db = mysql.connector.connect(host='**', user='**', password='**', database="**")
self.cursor = self.db.cursor()
self.root = customtkinter.CTk()
self.root.geometry('175x175')
self.root.title("登录或注册")
customtkinter.CTkButton(self.root, text='登录', command=self.login).pack(pady=20)
customtkinter.CTkButton(self.root, text='注册', command=self.reg_user).pack(pady=10)
self.root.mainloop()
def login(self):
self.login_window = customtkinter.CTk()
self.login_window.geometry('400x400')
self.login_window.title("登录")
self.login_username_entry = customtkinter.CTkEntry(self.login_window, placeholder_text='输入的用户名')
self.login_username_entry.pack(pady=30)
self.login_password_entry = customtkinter.CTkEntry(self.login_window, placeholder_text='输入的密码', show="*")
self.login_password_entry.pack(pady=30)
customtkinter.CTkButton(self.login_window, text='登录', command=self.log_user_in).pack(pady=30)
customtkinter.CTkButton(self.login_window, text='注册新用户', command=self.reg_user).pack(pady=20)
def log_user_in(self):
username = self.login_username_entry.get()
password = self.login_password_entry.get()
self.cursor.execute('SELECT userID, COUNT(*) FROM users WHERE username=%s AND password=%s GROUP BY username', (username, password))
result = self.cursor.fetchone()
if result and result[1] == 1:
user_id = result[0]
self.login_window.destroy() # 关闭登录窗口
habit_tracker = HabitTracker(self.root, user_id) # 使用主窗口作为父级
habit_tracker.main_menu()
else:
customtkinter.CTkLabel(self.login_window, text="用户未找到。请重试。", font=('Helvetica', 10)).pack(pady=10)
def reg_user(self):
# 在 此处添加用户注册逻辑
pass
if __name__ == "__main__":
login_reg = LoginOrReg()
说明:
-
分离窗口管理:
不要为每个窗口创建新的
CTk()
实例。在LoginOrReg
中,我们保留一个主root
窗口,并根据需要创建和销毁子窗口(如login_window
)。 -
登录成功后的流程:
在
log_user_in
中,成功登录后,我们创建HabitTracker
实例,将其传递给主root
窗口,然后调用habit_tracker.main_menu()
。 - 清除代码: 我删除了一些不必要的参数并将代码格式化得更清晰。
通过这些更改,的程序流程应该会得到改善,并且在登录后成功打开主菜单。
标签:python,python-3.x,file,oop,methods From: 78784053