0

I'm doing a calculator with Python 3.8 and Visual Studio. I'm trying to print the result in the label on button click.

This is the error:

Undefined variable 'resultado' pylint(undefined-variable) [37, 32]

This is my code:

from tkinter import *

r = Tk()
r.title("CALCULADORA")

def operar(num1, num2, signo):

    resultado = IntVar()

    if signo == "+":
        resultado = num1 + num2

    elif signo == "-":
        resultado = num1 - num2

    elif signo == "/":
        resultado = num1 / num2

    elif signo == "X":
        resultado = num1 * num2

    return resultado


textNumUno = Entry(r, width=7)
textNumUno.grid(row=0, column=0)

textOperacion = Entry(r, width=3)
textOperacion.grid(row=0, column=1)

textNumDos = Entry(r, width=7)
textNumDos.grid(row=0, column=2)

btn = Button(r, text="=", width=2, command=operar(textNumUno.get(), textNumDos.get(), textOperacion.get()))
btn.grid(row=0, column=3)

labelResultado = Label(r, text=resultado)
labelResultado.grid(row=0, column=4)

r.mainloop()
Mikein
  • 11
  • 1
  • resultado is local variable inside `operar()` - you have to create it outside `operar()` – furas Feb 16 '20 at 11:19
  • Read [Why is Button parameter “command” executed when declared?](https://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared) and [return value from function used in command](https://stackoverflow.com/a/13101037/7414759) – stovfl Feb 16 '20 at 12:49

2 Answers2

0

There are few mistakes


resultado is local variable - you have to create it outside function


IntVar(), (StringVar(), etc.) is not normal variable and it needs .get(), .set() to work with this

resultado.set( int(num1) + int(num2) )

print( resultado.get() )

Button doesn't work like input() - it doesn't wait for your answer so Label( text=) is created before you put text and press button. You can use textvarable= to assign IntVar() (StringVar(), etc.) and when you change value in IntVar then it will change text on Label.


Button will execute function later but it can't get result so using return in operar is useless.


command= need function name without () and arguments (so called callback) and later (when you press button) it will add () to this name to execute it. If you have to use function with arguments then you may use lambda to create function without arguments.

command=lambda:operar(textNumUno.get(), textNumDos.get(), textOperacion.get()))

Entry gives strings which you have to convert to int() (or float())


#from tkinter import * # PEP8: not preferred
import tkinter as tk

# --- functions ---

def operar(num1, num2, signo):

    if signo == "+":
        resultado.set( int(num1) + int(num2) )

    elif signo == "-":
        resultado.set( int(num1) - int(num2) )

    elif signo == "/":
        resultado.set( int(num1) / int(num2) )

    elif signo in "*xX":
        resultado.set( int(num1) * int(num2) )

# --- main ---

r = tk.Tk()
r.title("CALCULADORA")

resultado = tk.IntVar()

textNumUno = tk.Entry(r, width=7)
textNumUno.grid(row=0, column=0)

textOperacion = tk.Entry(r, width=3)
textOperacion.grid(row=0, column=1)

textNumDos = tk.Entry(r, width=7)
textNumDos.grid(row=0, column=2)

btn = tk.Button(r, text="=", width=2, command=lambda:operar(textNumUno.get(), textNumDos.get(), textOperacion.get()))
btn.grid(row=0, column=3)

labelResultado = tk.Label(r, textvariable=resultado)
labelResultado.grid(row=0, column=4)

r.mainloop()

BTW: as for import * see PEP 8 -- Style Guide for Python Code

furas
  • 134,197
  • 12
  • 106
  • 148
-1

I'm not familiar with the tkinter syntax, but text=resultado looks like an issue. That variable is local to the operar function, so it doesn't make sense to call it globally

mcindoe
  • 101
  • 2
  • 8