简体   繁体   中英

How to access variables from different class in tkinter to use in different functions?

I have a problem with accessing the data which is a different class. I have found some solutions in this platform but still, I could not implement it in my script. I will appreciate your help regarding this.

I tried to design a login page, including user_name and password. After the user fills those fields and click the login button, I want to get those variables (from page 1) to use in different functions(printing in def printName() ).

class Page(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)

    def show(self):
        self.lift()

class Page1(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs,bg='white')

        label = tk.Label(self, text="",bg='white')

        heading= tk.Label(self, text="ENTER OM", font=("ALGERIAN",40,"bold"), fg="black",bg='white').pack()
        user_name= tk.Label(self, text="User Name :", font=("arial",20,"bold"), fg="black",bg='white').place(x=15,y=201)
        password= tk.Label(self, text="Password :", font=("arial",20,"bold"), fg="black",bg='white').place(x=15,y=305)

        self.shared_data = {"last_user_name": tk.StringVar(),"password": tk.StringVar()}
        entry1 = tk.Entry(self, textvariable=self.shared_data["last_user_name"]).place(x=210, y=214)
        last_user_name = self.shared_data["last_user_name"].get()

        button_1=tk.Button(self,text="Log In", width=12,height=3,fg="white",bg="blue",font=("Arial",10), command=printName).place(x=350,y=450)

        button_3=tk.Button(self,text="Close", width=12,height=3,fg="white", bg="red", font=("Arial",10),command=close_window).place(x=180,y=450)
        label.pack(side="top", fill="both", expand=True)


class Page2(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        label = tk.Label(self, text="", bg="white")
        button_2=tk.Button(self,text="Om Creation", width=10,height=3 ,fg="white",bg="blue",font=("Arial",10), command=OMcreat).place(x=750,y=400)
        label.pack(side="top", fill="both", expand=True)


class Page3(Page):
    def __init__(self, *args, **kwargs):

        Page.__init__(self, *args, **kwargs)       
        label = tk.Label(self, text="")
        label.pack(side="top", fill="both", expand=True)


class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = Page1(self)
        p2 = Page2(self)
        p3 = Page3(self)

        buttonframe = tk.Frame(self)
        container = tk.Frame(self)
        root.configure(bg='white')
        buttonframe.pack(side="top", fill="x", expand=False)
        container.pack(side="top", fill="both", expand=True)

        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p3.place(in_=container, x=0, y=0, relwidth=1, relheight=1)

        b1 = tk.Button(buttonframe, text="Page 1", command=p1.lift)
        b2 = tk.Button(buttonframe, text="Page 2", command=p2.lift)
        b3 = tk.Button(buttonframe, text="Page 3", command=p3.lift)

        b1.pack(side="left")
        b2.pack(side="left")
        b3.pack(side="left")

        p1.show()

if __name__ == "__main__":
    root = tk.Tk()
    root.title("OM credantials")
    root.wm_geometry("1000x750+0+0")
    root.configure(bg='white')
    main = MainView(root)
    main.pack(side="top", fill="both", expand=True)

    root.mainloop()


def close_window():
    root.destroy()
    driver.close()


def printName():
    print(last_user_name)

You can either use global variables or just save the p1 class:

logvar=''
def save_login(l):
    global logvar
    logvar=l
def get_last_login():
    return logvar

Or use self.p1 instead of p1 in MainView and main.p1.shared_data['last_user_name'] The second method is better. Global variables are not recommended in general.

printName and close_window methods will be evaluated at last, unstead of declaring them outside a class move them to Page1 and change buttons commands:

class Page1(Page):
    def __init__(self, *args, **kwargs):
        ...
        button_1 = tk.Button(..., command=self.printName)
        button_3 = tk.Button(..., command=self.close_window)

    def printName(self):
        print(self.last_user_name.get())

    def close_window(self):
        root.destroy()
        driver.close()

You have a NameError: name 'OMcreat' is not defined in Page2 .

class Page2(Page):
    def __init__(self, *args, **kwargs):
        ...
        button_2 = tk.Button(..., command=self.OMcreat)
        button_2.place(x=750,y=400)

    def OMcreat(self):
        pass

The value of last_user_name should be empty until a user fills in it. replace it with

self.last_user_name = self.shared_data["last_user_name"]

and use get method when needed.

heading variable will be of None type which is the result of pack method (same with place method), so change it to the following and do the same with other variables:

heading= tk.Label(...)
heading.pack()

EDIT
The reference of the MainView instance is already available in args .To access pages instances, you can define them as an instance variable:

self.p1 = Page1(self)

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