Python Forum
[Tkinter] tkinter issue with variables carrying over between functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] tkinter issue with variables carrying over between functions
#1
Hi,

I suspect this is a very simple issue... I am trying to use a variable, loaded in one operation, within another operation. It is telling me the variable doesn't exist.

Below is the simplest way to reproduce my issue.

from tkinter import *

root = Tk()
root.title('Temp')
root.geometry('300x300')

def Import():
    u = 1
    v = 2

directoryButton = Button(root, text='Import', command=Import, padx=15)
directoryButton.grid(row=0,column=0 )

def Calc():
    t = u*v
    print(t)

directoryButton = Button(root, text='Calc', command=Calc, padx=15)
directoryButton.grid(row=0,column=1 )

root.mainloop()
Help would be massively appreciated.

Thanks,
Reply
#2
You could do something like this:
from tkinter import *
 
root = Tk()
root.title('Temp')
root.geometry('300x300')
u = 0
v = 0

def Import():
    global u, v
    u = 1
    v = 2
 
directoryButton = Button(root, text='Import', command=Import, padx=15)
directoryButton.grid(row=0,column=0 )
 
def Calc():
    t = u*v
    print(t)
 
directoryButton = Button(root, text='Calc', command=Calc, padx=15)
directoryButton.grid(row=0,column=1 )
 
root.mainloop()
For fun comment out the "global x, y" line and run the program. Notice any changes?

Functions can see variables from their enclosed scope (meaning Import and Calc can see the x and y that are assigned at the top of the module). However, when you don't tell Calc to use the global x, y it chooses to create new variables inside the function that just happen to have the same name.

global is fine to use for short programs, but as programs get bigger it becomes difficult keeping track of what is a local and what is a global variable. If you have multiple variables that are manipulated by multiple functions sometimes it makes sense to create a class that encapsulates the data and the functions.
class Example:
    def __init__(self, u=0, v=0):
        self.u = u
        self.v = v

    def Import(self, u, v):
        self.u = u
        self.v = v

    def Calc(self):
        return self.u * self.v

obj = Example(1, 2)
print(obj.calc())
This is too simple an example, but if you have a lot of operations that can be performed on your data a class may be a good choice.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 3,100 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 2,646 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  Super basic tkinter arduino issue Kurta 3 2,413 Jan-07-2021, 05:22 PM
Last Post: deanhystad
Photo tkinter issue mate 4 2,535 Dec-06-2020, 09:03 PM
Last Post: mate
  Issue in Tkinter with winfo_class() and LabelFrame ReDefendeur 1 2,741 Oct-05-2020, 05:52 AM
Last Post: Jeff900
  Help with tkinter gui and functions Omer_ 0 1,512 Sep-22-2020, 11:43 AM
Last Post: Omer_
  [Tkinter] tkinter: after issue edwin6938 1 3,397 Aug-25-2020, 04:37 PM
Last Post: Larz60+
  [Tkinter] How to compare two variables correctly in tkinter scratchmyhead 2 3,869 May-10-2020, 08:04 PM
Last Post: scratchmyhead
  Tkinter: increasing numbers and Radiobutton issue PeroPuri 1 2,163 Apr-13-2020, 05:48 PM
Last Post: deanhystad
  Issue on tkinter with buttons Reldaing 1 2,442 Jan-07-2020, 08:21 AM
Last Post: berckut72

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020