Can I convert the output of an object using get()?

Asked

Viewed 32 times

0

How do I convert the output of an object when using the get() function? Since it always returns a value string. Here’s a code I’m writing:

import tkinter as tk
import Sistema_backend

top = tk.Tk()
canvas = tk.Canvas(top, bg = "#159BA0", height = 500, width = 500)
canvas.pack()

Label1 = tk.Label(canvas, text = "Digite sua nota:")
Label1.place(x = 10,y = 10)
Entry1 = tk.Entry(canvas)
Entry1.place(x = 100,y = 10)

Label2 = tk.Label(canvas, text = "Digite sua nota:")
Label2.place(x = 10,y = 50)
Entry2 = tk.Entry(canvas)
Entry2.place(x = 100,y = 50)

Label3 = tk.Label(canvas, text = "Digite sua nota:")
Label3.place(x = 10, y = 90)
Entry3 = tk.Entry(canvas)
Entry3.place(x = 100, y = 90)

Label4 = tk.Label(canvas, text = "Digite sua nota:" )
Label4.place(x = 10, y = 130)
Entry4 = tk.Entry(canvas)
Entry4.place(x = 100, y = 130)

Label5 = tk.Label(canvas, text = "Média mínima: ")
Label5.place(x=10, y = 170)
Entry5 = tk.Entry(canvas)
Entry5.place(x=100, y = 170)

Label5 = tk.Label(canvas, text = "Total:")
Label5.place(x = 10,y = 210)
Label6 = tk.Label(canvas, text = "Resultado")
Label6.place(x = 10,y = 250) 

try:
    Button1 = tk.Button(canvas, text = "Enviar notas",
    command = Sistema_backend.Sistema(Entry1.get(float), 
    Entry2.get(float), Entry3.get(float), Entry4.get(float), Entry5.get(float)))
except:
    print('Algo de errado aconteceu!')
finally:
    Button1 = tk.Button(canvas, text = "Enviar notas")
    Button1.place(x = 10, y = 210)


Button2 = tk.Button(canvas, text = "Passou?")
Button2.place(x=10 , y= 290)

Label_result = tk.Label(canvas,text = "Resultado")
Label_result.place(x=10, y = 330)




canvas.mainloop()

More precisely in this part:

try:
    Button1 = tk.Button(canvas, text = "Enviar notas",
    command = Sistema_backend.Sistema(Entry1.get(), 
    Entry2.get(), Entry3.get(), Entry4.get(), Entry5.get()))
except:
    print('Algo de errado aconteceu!')
finally:
    Button1 = tk.Button(canvas, text = "Enviar notas")
    Button1.place(x = 10, y = 210)

Basically, in the GUI that I imported, when the user presses "Button1", he will send the data. These data is obtained from "Entry" (type an input, Entry is just the name the library gives). The function get() will then allow me to "extract" the data in Entry, but the problem is that it returns in STR, and i want to work with FLOAT but don’t know how to convert.

1 answer

2

Use the function float(). It’ll stay that way:

command = Sistema_backend.Sistema(float(Entry1.get()), 
    float(Entry2.get()), float(Entry3.get()), float(Entry4.get()), float(Entry5.get()))

If no numerical value is entered in Entry an exception will be raised. The number must be inserted using point to separate decimal place. If you use comma will give error too.

  • Dude, I tried that which you said but it wasn’t. It gives the error even before receiving, so I tried to put a lambda, and it worked. But thanks just the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.