1

The code the problem is part of is fairly big so I'm drafting a fingerpainting version here.

import tkinter

variable = "data"

def changeVariable():
    variable = "different data"

def printVariable():
    print(variable)

window = tkinter.Tk
button1 = tkinter.Button(window, command=changeVariable)
button1.pack()
button2 = tkinter.Button(window, command=printVariable)
button2.pack()

So in this example, I press the first button to change 'variable', then the second button to print it. But "data" is printed instead of "different data". I searched around a bit and decided to use global before defining the variable in both the main code and in the functions, so the code looked like this.

import tkinter

global variable
variable = "data"

def changeVariable():
    global variable
    variable = "different data"

def printVariable():
    global variable
    print(variable)

window = tkinter.Tk()
button1 = tkinter.Button(window, command=changeVariable)
button1.pack()
button2 = tkinter.Button(window, command=printVariable)
button2.pack()

window.mainloop()

But now it says 'name 'variable' is not defined'.

Essentially, how can I get the variable 'variable' to change with a button in tkinter? Was I wrong to think of using global?

4
  • 1
    I think you don't need the first global variable. Commented May 31, 2017 at 19:41
  • Your code works for me. Are you sure you didn't misspell "variable" in there somewhere? Commented May 31, 2017 at 19:43
  • Your second example works for me once I make two changes: window = tkinter.Tk() and add window.mainloop() at the end. Commented May 31, 2017 at 19:54
  • 1
    What @quamrana said: the global directive is used inside a function to tell the function to look up the name in the global namespace. It doesn't make sense to use it in the global namespace itself. But really, it's a good idea to avoid using modifiable globals because they break program modularity. The neat way to do this is to define your GUI as a class, and then you can use instance attributes to save state data. BTW, it's a common practice to do import tkinter as tk. Then you can do stuff like button = tk.Button(window). Commented May 31, 2017 at 20:03

1 Answer 1

2

Your use of global is a bit off. You do not need to define global all over the place. Lets break it down a little.

You don't need to define global namespace in the global namespace.

from tkinter import *
window = Tk()
myvar = "data" # this variable is already in the global namespace

This tells the function to check global namespace when its interacts with the variable myvar.

def changeVariable():
    global myvar
    myvar = "different data"

This print statement works because it checks the global variable namespace after it has check the other namespaces without finding the variable myvar.

def printVariable():
    print(myvar)

button1 = Button(window, command = changeVariable)
button1.pack()
button2 = Button(window, command = printVariable)
button2.pack()

window.mainloop()

So if we put this code together we will get the desired result.

from tkinter import *
window = Tk()
variable = "data"

def changeVariable():
    global variable
    variable = "different data"

def printVariable():
    print(variable)

button1 = Button(window, command = changeVariable)
button1.pack()
button2 = Button(window, command = printVariable)
button2.pack()

window.mainloop()

This results in a window that looks like this:

enter image description here

and the result if we press the bottom button first then the top button then the bottom button again we get:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.