简体   繁体   中英

Python - accessing a local variable from another class

I have tried to condense the code down as much as possible to make it clear what I am asking...

I have a variable called chosen_name , determined in a class called booking_frame , that I would like to access in the calendar_frame class.

Therefore, it would be obvious for calendar_frame to inherit the attributes of booking_frame - however, I believe (I'm probably completely wrong lol) that calendar_frame has to inherit the characteristics of Frame so that the whole program functions correctly.

The reason that calendar_frame is a completely separate class is so that it can appear as a different frame.

Extremely grateful for any help given:)


# import tkinter modules
from tkinter import *
from tkinter import ttk
import tkinter.font as tkFont
from PIL import ImageTk, Image
from tkcalendar import *



# define self
class tkinterApp(Tk):

    def __init__(self,*args, **kwargs):

        Tk.__init__(self, *args, **kwargs)

        # creating a container
        container = Frame(self)
        container.pack(side = "top", fill = "both", expand = True)

        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)

        # initialising frames to an empty array
        self.frames = {}

        menu_bar = Menu(container)
        main_menu = Menu(menu_bar)

        menu_bar.add_cascade(label="Main Menu", menu=main_menu)
        main_menu.add_command(label="Welcome page", command=lambda: self.show_frame(welcome_frame))
        main_menu.add_command(label="Book a vehicle", command=lambda: self.show_frame(booking_frame))
        main_menu.add_command(label="Register as new user", command=lambda: self.show_frame(register_frame))

        Tk.config(self, menu=menu_bar)

        for F in (welcome_frame, booking_frame, register_frame, calendar_frame):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")

        self.show_frame(welcome_frame)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class welcome_frame(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)

        welcome = Label(self, text="Hello, please use the menu above to navigate the interface")
        welcome.grid(row=0, column=4, padx=10, pady=10)

class register_frame(Frame):

    def __init__(self, parent, controller):

        Frame.__init__(self, parent)

        register_label = Label(self, text="New user - enter your details below to use the Collyer's car park.")
        register_label.grid()


class booking_frame(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)

        chosen_name = "Steve"


class calendar_frame(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)

    print(booking_frame.chosen_name)


app = tkinterApp()
app.geometry("1000x800")
app.title("Collyer's Car Park")
app.mainloop()

First you need to change local variable chosen_name to instance variable self.chosen_name inside booking_frame class, otherwise it cannot be accessed outside the class:

class booking_frame(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)

        self.chosen_name = "Steve"  # changed to instance variable

Then you can access it via controller.frames[booking_frame].chosen_name inside calendar_frame class:

class calendar_frame(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)

        print(controller.frames[booking_frame].chosen_name)

Inheritance is to model relationships of objects which behave the same ("IS A" relationship). It is not meant to share data between objects.

A possible solution to your problem is to use a third object, that would be shared between booking_frame and calendar_frame .

This object can be a Python dictionary for example; you can pass it to all your "frame" objects, or you can maybe decide to have it global (not very academic, for sure, but quick and dirty):

GLOBAL_STATE = {}

class booking_frame(Frame):
    ...
    GLOBAL_STATE["chosen_name"] = "Steve"

class calendar_frame(Frame):
    ...
    print(GLOBAL_STATE.get("chosen_name"))

I hope you can see now how you can refactor your code to share data between those objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM