0

I'm a beginner in python, and was trying to create a basic GUI calculator using Tkinter module. My code gives me an error UnboundLocalError: local variable 'display' referenced before assignment even when I've assigned the value to the variable in the beginning of the code. Here is my code, any help will be appriciated.

display = ""
flag = 0

def set():
    display = display + str(a)
    if flag == 0:
        calc1 = float(display)
    elif flag == 1:
        calc2 = float(display[len(str(calc1)) - 1:END])
    label.config(text = display)
    print (calc1)
    print (calc2)
    print (display)

def set0():
    a=0
    set()

# similar functions for values 1-9

set0()
Prune
  • 76,765
  • 14
  • 60
  • 81
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. – Prune May 24 '17 at 20:48

1 Answers1

1

The problem appears to be here:

def set():
    display = display + str(a)

Since you didn't declare display to be global, this is a local variable. It's uninitialized. I'm not 100% certain, since you didn't include the full error message, but this line will certainly elicit a fatal error.

You can reference a global variable without the declaration, but you can't change its value.

Note: you also have a definition problem with a in the set function: there's no such variable here.

Prune
  • 76,765
  • 14
  • 60
  • 81