-1

What I am trying to do:

A GUI with tkinter to covert Celsius to Fahrenheit.

What is happening

The window itself is working but then when I try to call the calc function with a button it returns an error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.6/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
TypeError: calc() missing 1 required positional argument: 'grausC'

My code:

from tkinter import *

janela = Tk()
janela.title("Conversao")
janela.geometry("600x600")

def calc(grausC):
    graus = float(grausC.get())
    Fahrenheit = (graus * 9/5 + 32)
    lb3=Label(janela, text=CalcF)
    lb3.place(x=200, y=200)


titulo=Label(janela, text="Conversao de Celsius para Fahrenheit", font=("Verdana 20 underline"))
titulo.place(x=20, y=20)

grausC = Entry(janela)
grausC.place(x=200,y=150)

lb1=Label(janela, text="Graus em Celsius:")
lb1.place(x=70, y=150)

lb2=Label(janela, text="Graus em Fahrenheit:")
lb2.place(x=70, y=200)

btn=Button(janela, text= "Calcular", command=calc) 
btn.place(x=100, y = 250)

janela.mainloop()
J_Zoio
  • 338
  • 1
  • 3
  • 16
  • I guess you have to make sure that whatever parameter you need is actually passed to `calc`. – Stefan Falk Dec 07 '18 at 09:50
  • 2
    Maybe look at this: https://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter – Stefan Falk Dec 07 '18 at 09:52
  • @StefanFalk How is it not? The link is not quite what I am looking for... Thanks for tryong though! – J_Zoio Dec 07 '18 at 09:56
  • @buran I would agree if this was not a specific error that that thread does not particularly explain. I have looked at it prior to posting my question. – J_Zoio Dec 07 '18 at 10:01
  • 3
    According to your code, you can just remove the parameter `grausC` from `calc()` so that `grausC` inside `calc()` refers to the global one. You can also use `lambda` for `command` parameter: `command=lambda x=grausC: calc(x)`. – acw1668 Dec 07 '18 at 10:02

1 Answers1

2

Solved my problem. Function was badly defined.

def calc():
    graus = float(grausC.get())
    Fahrenheit = (graus * 9/5 + 32)
    FahrenheitS = str(Fahrenheit)
    lb3=Label(janela, text=FahrenheitS)
    lb3.place(x=200, y=200)
J_Zoio
  • 338
  • 1
  • 3
  • 16