Home > Mobile >  How to avoid global variables in tkinter?
How to avoid global variables in tkinter?

Time:11-04

I've recently been using Tkinter and I'm usually running into this problem:

Let's say I have to clear a label every time I press a button to show something different to avoid the previous output mixing with the new content of the label. I made a code example to show what I mean:

from tkinter import *

root = Tk()
root.minsize(200, 200)


def click():
    global label_1


    if entry_1.get() == '1':
        label_1 = Label(root, text="something")
        label_1.grid(row=2, column=0)

    elif entry_1.get() == '2':
        label_1.grid_remove()
        label_2 = Label(root, text="different")
        label_2.grid(row=2, column=0)


entry_1 = Entry(root)
entry_1.grid(row=0, column=0)
button_1 = Button(root, text='click', command=click)
button_1.grid(row=1, column=0)

root.mainloop()

First of all, I know if 2 is passed first, it'll throw an error but that's not the problem.

My point is if we remove the global variable then we won't be able to remove the previous output and when 2 is passed the old label content will be still there so it'll look mixed with the new one.

I know this can be easily handled with OOP but, is there another way to do this? Because I know using global variables is bad practice but I run into this problem very often and the only way I can think of is global variables and of course OOP but that means refactoring the entire code. Or in this case, is it not that bad to use globals?

Thanks in advance, I'm still learning Tkinter but the guy on the course is always using globals to solve this kind of scenario.

CodePudding user response:

Does this help? This will prevent global. I put two widget Labels outside of function You will see no effect on Label.

from tkinter import *

root = Tk()
root.minsize(200, 200)

 
def click():
    if entry_1.get() == '1':         
        label_1.grid(row=2, column=0)
        label_2.grid_remove()

    elif entry_1.get() == '2':
        label_1.grid_remove()         
        label_2.grid(row=2, column=0)
         
    
label_1 = Label(root, text="something")#
label_2 = Label(root, text="different")    

 
entry_1 = Entry(root)
entry_1.grid(row=0, column=0)

button_1 = Button(root, text='click', command=click)
button_1.grid(row=1, column=0)

root.mainloop()

Result type 1:

enter image description here

Result type 2:

enter image description here

CodePudding user response:

Instead of using global variables for the Widgets, you can attach them as attributes to the Tk()-object root. So, they are accessible whenever root is.

from tkinter import *

root = Tk()
root.minsize(200, 200)


def click():

    if root.entry_1.get() == '1':
        root.label_1 = Label(root, text="something")
        root.label_1.grid(row=2, column=0)

    elif root.entry_1.get() == '2':
        root.label_1.grid_remove()
        root.label_2 = Label(root, text="different")
        root.label_2.grid(row=2, column=0)


root.entry_1 = Entry(root)
root.entry_1.grid(row=0, column=0)
root.button_1 = Button(root, text='click', command=click)
root.button_1.grid(row=1, column=0)


root.mainloop()
  • Related