Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

python - Referencing one class object from another

I've not used Python for a while... so I'm rusty. How do I reference one object from another class? (I'm a bit tired lol)

class UI_Controller(tk.Tk):
    def __init__(self, *args, **kwargs):
        self.name = 'Kitty'

class PetPage(tk.Frame):
    def __init__(self, parent, controller):
        print(name) #<<<<< ??

full code - the part I want to print is 'print(name)' damn... I need to add more text now... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders... screw Flanders...

#has working frames, 
import tkinter as tk
fullscreen_fullscreen = False
import csv
import os
#locals()
#codePet project

class UI_Controller(tk.Tk):


    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, 'Code Pet')
        tk.Tk.geometry(self, '400x600')  # This is the size of the screen (in pixels)

        container = tk.Frame(self)
        container.pack(fill='both', expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)



        if fullscreen_fullscreen:
            tk.Tk.wm_attributes(self, "-fullscreen", True)  # turn on and off fullscreen.

        self.frames = {}
        
        #below are the frames for the game
        for F in (PetPage,
                  MainGame):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

            frame.configure(background='ivory2'),

        self.grid_controller()
        self.show_frame(PetPage)

        global self.name = 'Kitty'
        self.hunger = '001'
        self.thirst = '001'
        self.tired = '001'
        self.poop = '001'
        self.play = '001'
        self.angry = '001'
        self.happy = '001'
        self.sad = '001'

        #here we have a list of main var's
        self.name = 'Kitty'
        self.hunger = '001'
        self.thirst = '001'
        self.tired = '001'
        self.poop = '001'
        self.play = '001'
        self.angry = '001'
        self.happy = '001'
        self.sad = '001'

        #if os.path.isfile("/memory") == True:
        try:
            self.load_creature(self)
        except:        self.write_creature_to_file(self)

        #the above is an example of a var stored and changed later. :)

    def load_creature(self, parent):
        with open('memory', "r") as f:
            list2 = []
            for item in f:
                number = 0
                while number < 1:
                    list2.append(item)
                    number += 1
            self.name = list2[0]
            self.hunger = list2[1]
            self.thirst = list2[2]
            self.tired = list2[3]
            self.poop = list2[4]
            self.play = list2[5]
            self.angry = list2[6]
            self.happy = list2[7]
            self.sad = list2[8]





    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
    def grid_controller(self):
        #Alpha = list(string.ascii_lowercase)
        #print(Alpha[1])
        pass
        #taken out.


    def write_creature_to_file(self, parent):
        file = open('memory','w') 
        file.write('''%s
%s
%s
%s
%s
%s
%s
%s
%s''' % (self.name, self.hunger, self.thirst, self.tired,
             self.poop, self.play, self.angry, self.happy, self.sad))

        
        file.close()       




class PetPage(tk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        tk.Frame.__init__(self, parent)
        
        sysstatbutton = tk.Button(self, text='System Status',
                                  command=lambda: controller.show_frame(MainGame),
                                  height=2, width=12)
        sysstatbutton.place(x=20, y=180)

        #new text box

        mainText = tk.Text(self, height=8, width=40)
        mainText.place(x=20, y=400) #x is along, y is down, obviously lol
        #below inserts the text
        #mainText.insert(tk.END, "%s" % UI_Controller.name) #
 is line down, obvs ^^
        print(name)
        #

class MainGame(tk.Frame, UI_Controller):
    def __init__(self, parent, controller):
        self.controller = controller
        tk.Frame.__init__(self, parent)
        sysstatbutton = tk.Button(self, text='page 2',
                                  command=lambda: controller.show_frame(PetPage),
                                  height=2, width=12)
        sysstatbutton.place(x=20, y=180)

if __name__ == '__main__':

    app = UI_Controller()
    app.mainloop()
    app = None
    exit()






'''with open('memory', newline='') as inputfile:
            results = list(csv.reader(inputfile))
            #print(results) #[3]
            importlist = [parent.name, parent.hunger, parent.thirst, parent.tired,
                          parent.poop, parent.play, parent.angry,
                          parent.happy, parent.sad]
            number = 1
            name = importlist[0] 
            for i in importlist:#needswork
                importlist[number] = '{:03}'.format(int(results[number][0][0]) + int(results[number][0][1]) + int(results[number][0][2]))
                print(importlist[number])
                number = number + 1
'''







            
'''parent.hunger = results[0][0][0]
            parent.thirst = results[1][0][0]
            parent.tired = results[2][0][0]
            parent.poop = results[3][0][0]
            parent.play = results[4][0][0]
            parent.angry = results[5][0][0]
            parent.happy = results[6][0][0]
            parent.sad = results[7][0][0]
            #parent.sad = int(self.sad) + 5
            '''
question from:https://stackoverflow.com/questions/65617492/referencing-one-class-object-from-another

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The way you do it is to reference the object. Since PetPage is being given an instance of the controller, then print(self.controller.name) should work.

In your code, however, you're not defining self.name until after you create the instance of PetPage. You need to define self.name before you create any objects that use it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...