Search code examples
pythonclass

cant figure out how to get a variable from a subrouting inside one class into another class


Have started experimenting with classes recently and have been trying to figure out how to get the variable high_score from one class (called Login) to another (Game)

Here is where the high_score variable is created in a subroutine inside the Login class

def enter(self):
import sqlite3
from user_data import Database
from tkinter import messagebox

        db = Database()
        self.username = self.username_entry.get()
        self.password = self.password_entry.get()
        sql = f'SELECT UserPassword, High_Score FROM user WHERE UserName = "{self.username}"'
        db.c.execute(sql)
        result = db.c.fetchall()
        print(result)
        real_password = result[0][0]
        self.high_score = result[0][1]
        print(real_password)
        if self.password == real_password:
            messagebox.showinfo('Login Status',f'Account found High score = {self.high_score}') 
            self.open_game()
            return self.high_score

And here is where I am attempting to call it in a seperate class called Game

from login import Login
import tkinter as tk
from tkinter import messagebox

class Game:
    def __init__(self):
        self.root = tk.Toplevel()
        self.root.geometry('1080x720')

        self.High_score_label = tk.Label(self.root, width=20, font=('Times New Roman', 20, 'bold'), text='High Score')
        self.High_score_label.place(x=440, y=150, width=200, height=50)
        self.High_score_num = tk.Label(self.root, width=20, font=('Times New Roman', 20, 'bold'), text=Login.self.high_score)

when attempting to run the code I get the error "AttributeError: type object 'Login' has no attribute 'self'"

I have tried multiple things but am struggling to figure out what i need to do as i am new to class use in python. Have tried looking at what other people did but cannot figure out how to apply it to my specific case.


Solution

  • As @Barmar says, self represents the instance of class. To fix your code, you need to decide is how you are passing the user to the Game class.

    However, to deal with your error message. For each class, you use 'self' to represent the instance of that class, so Login in Game class is outside of its class definition, so you drop the self attribute. That's the lesson from this error. That won't stop the errors, however.

    So, how are you passing the user to the Game is something you need to decide. In the code below, I answer it to allow me to provide a full correction. I assume that you pass the user to the Game as you initialise it, but you might choose a different path.

    class Game:
        def __init__(self, user: Login):
            self.root = tk.Toplevel()
            self.root.geometry('1080x720')
    
            self.High_score_label = tk.Label(self.root, width=20, font=('Times New Roman', 20, 'bold'), text='High Score')
            self.High_score_label.place(x=440, y=150, width=200, height=50)
            self.High_score_num = tk.Label(self.root, width=20, font=('Times New Roman', 20, 'bold'), text=user.high_score)
    

    As @Barmar says, you need to intialise Login, and then use the enter method to populate it. When you do that and then use it to initialise Game, the edit should now work