-4

I am trying to pass a variable called "data" From a class called LoginOrReg in a file called LoginOrReg to another class in another file Called HabitTracker, but i get a programming error

Here is what i believe to be the minimal reproducible code. I have created the variable in the method called "log_user_in"

from tkinter import *
import mysql.connector
import customtkinter
import math
from datetime import datetime 
import HabitTracker as HT



#Handles login and registration
class LoginOrReg:

    customtkinter.set_appearance_mode('dark')
    customtkinter.set_default_color_theme('blue')
    

    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
  #provides the login screen where the user is asked for an existing username and password
    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).pack(pady=30)
        self.reg_button = customtkinter.CTkButton(self.login_window, text = 'Register New User', command = self.reg_user).pack(pady=20)
        self.login_window.mainloop()

    #Logs the user in
    def log_user_in(self):
        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)
            self.user_id = self.data[0]
            self.habit.main_menu(LoginOrReg)
        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=" ")

Here is where I try to call the "data" variable in HabitTracker in the method Main_menu.

from tkinter import *
import mysql.connector
import customtkinter
import math
from datetime import datetime
import LoginOrReg as LR


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, LR):
        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()

Here is the error I get

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1264.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 1968, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\Administrator\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\customtkinter\windows\widgets\ctk_button.py", line 554, in _clicked
    self._command()
  File "C:\Users\Administrator\Desktop\Scanes-Kael_9213430_OOFPP_Habit\Scanes-Kael_9213430_OOFPP_Habit_Submission\Kael OOFPP_Phase 2\LoginOrReg.py", line 84, in log_user_in
    self.habit.main_menu(LoginOrReg)
  File "C:\Users\Administrator\Desktop\Scanes-Kael_9213430_OOFPP_Habit\Scanes-Kael_9213430_OOFPP_Habit_Submission\Kael OOFPP_Phase 2\HabitTracker.py", line 30, in main_menu
    self.cursor.execute('SELECT habit_name FROM habit WHERE user_id=%s GROUP BY habit_name', (self.login.data))
  File "C:\Users\Administrator\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\mysql\connector\cursor.py", line 526, in execute
    raise ProgrammingError(
mysql.connector.errors.ProgrammingError: Could not process parameters: str( ), it must be of type list, tuple or dict
3
  • 1
    This does not look minimal to me. stackoverflow.com/help/minimal-reproducible-example "Restart from scratch. Create a new program, adding in only what is needed to see the problem." Commented Jul 29, 2024 at 14:37
  • self.login.data is a string after login_reg = LoginOrReg(db = " ", cursor=" ", login_username= ' ', data = ' ') Commented Jul 29, 2024 at 14:40
  • Maybe this answer can help stackoverflow.com/a/54518905/1839624 Commented Jul 29, 2024 at 14:41

1 Answer 1

1

This is the offending line of code:

self.cursor.execute('SELECT habit_name FROM habit WHERE user_id=%s GROUP BY habit_name', (self.login.data))

The second parameter, (self.login.data) is indeed not a list, a tuple or a dict - it's self.login.data surrounded by parentheses.
Add a comma after the variable to make it a single-item tuple:

(self.login.data,)
# Here ---------^
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.