-1

I have two classes(frames) for Tkinter one class generates a user id variable by using api inside a bbutton command method now i want to use that into another class how can i do it i have defined that variable global but not working. my code is something like this

class login page(tk.Frame):
        def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
             def button_function():
                 global user_id 
                 user_id  = some value i got from api response
             button = tk.button(command = button_fucntion())

class destination_page(tk.Frame):
        def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
              def another_button_function(user_id):
                  #i want that user_id value here 

              button_2 = tk.button(command = another_button_fucntion(user_id))
lex
  • 63
  • 7

1 Answers1

0

This code is able to access user_id and print it.

Buttons must be pressed in the correct order to prevent an error so this is a problem that needs to be addressed.

I've simplified it since I have no idea what controller does.

import tkinter as tk

class login_page(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        def button_function():
            global user_id
            user_id  = "some value i got from api response"
        #
        self.button = tk.Button(
            self, text = "Click me first", command = button_function)
        self.button.grid(sticky = tk.NSEW)

class destination_page(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        def another_button_function(user_id):
            print(user_id) #i want that user_id value here 
        #
        self.button_2 = tk.Button(
            self, text = "Click me second",
            command = lambda: another_button_function(user_id))
        self.button_2.grid(sticky = tk.NSEW)

master = tk.Tk()
app1 = login_page(master, "?")
app1.grid(sticky=tk.NSEW)

app2 = destination_page(master, "?")
app2.grid(sticky=tk.NSEW)

master.mainloop()
Derek
  • 1,916
  • 2
  • 5
  • 15