0

I'm trying to share variables from tkinter class to the global code, I have a tkinter gui in which I insert data and once closed the tkinter form I would like the code to print this data, but the code doesn't take the data I entered,and returns another value.

this is the code:

class App:

    def __init__(self, master):

        self.username = None
        self.frame1 = tk.LabelFrame (master, text="Instagram")
        self.frame1.pack ()
        self.dict = {'':[], 'Scienza': ['Nasa', '2', '3'],
                    'Moda': ['vladislavkirillov', 'barengo1', 'vittorio__badia'],
                    'Dunk Page': ['stupidpikachu', 'nascecresceignora', 'the_fucking_dank']}

        self.categoria_pagina = tk.StringVar()
        self.pagina_preimpostata = tk.StringVar()
        self.username1 = tk.StringVar()
        self.password = tk.StringVar()
        self.pagina_tua = tk.StringVar()
        self.commento1 = tk.StringVar()

        Entry (self.frame1, textvariable=self.username1, bg="white").grid (column=1, row=2, sticky=tk.W)

        Entry (self.frame1, textvariable=self.password, bg="white").grid (column=1, row=3, sticky=tk.W)

        Entry (self.frame1, textvariable=self.pagina_tua, bg="white").grid (column=1, row=4, sticky=tk.W)

        self.categoria_pagina.trace('w', self.updateoptions)

        self.optionmenu_a = ttk.OptionMenu(self.frame1, self.categoria_pagina, *self.dict.keys())
        self.categoria_pagina.set("Scienza")
        self.optionmenu_a.grid(row = 7, column = 1)

        self.optionmenu_b = ttk.OptionMenu(self.frame1, self.pagina_preimpostata, '')
        self.optionmenu_b.grid(row = 8, column = 1)


        self.variable_c = tk.StringVar ()
        self.variable_d = tk.StringVar ()


    def updateoptions(self, *args):
        countries = self.dict[self.categoria_pagina.get()]
        self.pagina_preimpostata.set(countries[0])
        menu = self.optionmenu_b['menu']
        menu.delete(0, 'end')
        for country in countries:
            menu.add_command(label=country, command=lambda country=country: self.pagina_preimpostata.set(country))


        categoria = self.categoria_pagina.get()
        pagina_preimpostata_stringa = self.pagina_preimpostata.get()
        username= self.username1.get()
        password= self.password.get()
        pagina_tua=self.pagina_tua.get()

    root = tk.Tk()

    app = App(root)

    root.mainloop()

    print(app.username)

it returns value None in place of the value that I insert in the tkinter gui

4
  • The error is clear , what is username? Commented Jun 26, 2018 at 12:13
  • username= self.username1.get () why a space ? is this a typo ? Commented Jun 26, 2018 at 12:14
  • no ,without space. the problem is that the code doesn't recognize the variable when i exit from the tkinter gui. If i try to print the variable adding the command print(username) to a button in the tkinter gui it works Commented Jun 26, 2018 at 12:18
  • 1
    "but the code doesn't take the data I entered,and give me and error." - what's the error? Commented Jun 26, 2018 at 14:00

1 Answer 1

1

The issue is that when you do below in your code

username= self.username1.get()

Python creates a local variable in the scope of the function and when the function ends the variable is no longer there. So you have multiple was to solved this.

One is to define username in __init__ of App

self.username = None

And in your function use

self.username = self.username1.get()

And in later you can use print(app.username)

Another option is to use global variables (NOT RECOMMENDED)

username = None

class App
.....
    global username
    username= self.username1.get()

....

print(username)
Sign up to request clarification or add additional context in comments.

4 Comments

i tried to do as u said , i declared self.username=None in __init__ but whe i tried to do print(app.username) it returns value ` None` and not the value that i insert in the tkinter gui. With the global variable it works but if u said that it's not recommended i would like to find another way
Can you make sure your updateoptions is actually being called? Also please update complete code with import statements so one can debug
ye thanks it works, but do you have an idea of why it doesn't work with print(app.username)?
Because updateoptions is call when you have self.categoria_pagina.trace('w', self.updateoptions) and not after that. You want values after closing of the form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.