Python Forum
Need help with an error - Printable Version

+- Python Forum (
+-- Forum: Python Coding (
+--- Forum: GUI (
+--- Thread: Need help with an error (/thread-25287.html)

Need help with an error - Miguel_Lona605 - Mar-25-2020

Sorry, this is probably a dumb question but im still studing python
im making a calculator with guizero and i have this error

>>> Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files\Mu IDE\pkgs\tkinter\", line 1705, in __call__ return self.func(*args) File "C:\Program Files\Mu IDE\pkgs\guizero\", line 197, in _command_callback self._command() File "c:\users\dell\mu_code\phase", line 16, in one screen_numbers = screen_numbers + '1' UnboundLocalError: local variable 'screen_numbers' referenced before assignment
what do i do to fix it?
Im new in the comunity btw

from guizero import App, Box, PushButton, Text
app = App(title="Project Sigma", bg="white", height=299, width=320)


# Screen
screen_topside = Box(app, height=60, width=309, border=1, align="top")
screen_numbers = Text(screen_topside, height='fill', width='fill')
screen_numbers.text_size = 21

# Space between screen and numpad
space = Box(app, height=10, width=330, align="top")

# Functions
def one():
    screen_numbers = screen_numbers + '1'

# Number Pad
numpad = Box(app, height=223, width=309, layout='grid', border=1)

buttonc = PushButton(numpad, text='C', height=1, width=7, grid=[0,0])
buttond = PushButton(numpad, text='÷', height=1, width=7, grid=[1,0])
buttonm = PushButton(numpad, text='×', height=1, width=7, grid=[2,0])
button_del = PushButton(numpad, text='Del', height=1, width=7, grid=[3,0])
button1 = PushButton(numpad, text="1", command=one, height=1, width=7, grid=[0,1])
button2 = PushButton(numpad, text="2", height=1, width=7, grid=[1,1])
button3  = PushButton(numpad, text="3", height=1, width=7, grid=[2,1])
button_minus = PushButton(numpad, text='-', height=1, width=7, grid=[3,1])
button4  = PushButton(numpad, text="4", height=1, width=7, grid=[0,2])
button5  = PushButton(numpad, text="5", height=1, width=7, grid=[1,2])
button6  = PushButton(numpad, text="6", height=1, width=7, grid=[2,2])
button_plus = PushButton(numpad, text='+', height=1, width=7, grid=[3,2])
button7  = PushButton(numpad, text="7", height=1, width=7, grid=[0,3])
button8  = PushButton(numpad, text="8", height=1, width=7, grid=[1,3])
button9  = PushButton(numpad, text="9", height=1, width=7, grid=[2,3])
button_same = PushButton(numpad, text='=', height=4, width=7, grid=[3,3,4,3])
button0  = PushButton(numpad, text="0", height=1, width=18, grid=[0,4,2,4])
button_dot = PushButton(numpad, text='.', height=1, width=7, grid=[2,4])

RE: Need help with an error - michael1789 - Mar-25-2020

You are referring to a global screen_numbers from within a function so it is looking for a variable in the function, which there isn't.

The best is to give the variable to the function as an argument and have it return the answer.

screen_numbers = Text(screen_topside, height='fill', width='fill')

def one(screen_numbers):
    screen_numbers = screen_numbers + '1'
    return screen_numbers

new_screen_numbers = one(screen_numbers)

RE: Need help with an error - Miguel_Lona605 - Mar-25-2020

Thanks, but to solve it i first set the variable to global outside the function and i used
global variablename
inside the function

RE: Need help with an error - joe_momma - Mar-26-2020

Quote:screen_numbers = Text(screen_topside, height='fill', width='fill')
your screen_numbers is a Text widget to manipulate the text you use value like so:
count= 1 #used to keep track of the number of ones will need to be reset
# Functions
def one():
    global count    
    screen_numbers.value= '1' * count
    count +=1
all numbers added to your screen_numbers are strings and will need to be converted to integers to evaluate their expressions. check out the module partial, it allows you to pass arguments to your number and other buttons- best of luck