Я пытаюсь запустить файл Python под названием HabitTracker из другого файла под названием LoginOrReg, но когда я это делаю, он повторно запускает файл 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)
Как вы можете видеть, я импортировал файл Habit Tracker как HT в свой метод «log_user_in».
Когда я нажимаю кнопку входа в систему, предусмотренную в функции входа в систему, я не думаю, что он даже запускает функцию Log_user_in, поскольку оператор печати, который я туда поместил, не отображается в терминале. Моя цель — запустить метод Main_menu в файле HabitTracker.
Здесь я импортирую файл и класс LoginOrReg в свой основной файл:
class HabitTracker:
def __init__(self, main_window, sorting_options, sort, login) -> None:
self.main_window = main_window
self.db = mysql.connector.connect(host = 'yourneighbourhoodscientist.co.za', user = 'hoodscientist_hoodscientist', password= 'Kscanes3394#', database="hoodscientist_HabitTracker")
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()
Подробнее здесь: https://stackoverflow.com/questions/787 ... other-file