Posts: 10
Threads: 2
Joined: Apr 2019
Hello guys, i am trying to create a programm with tkinter (for first time) and i got the follow error in one part of my code:
y = StringVar()
z = StringVar()
entry_1 = Entry(root, textvariable=y)
entry_1.grid(row=0, column=1)
entry_2 = Entry(root, textvariable=z)
entry_2.grid(row=1, column=1)
y = str(y.get())
z = str(z.get())
y1=eval(y)
z1=eval(z) When i run this loop i get the error "SyntaxError: unexpected EOF while parsing".
If i use float instead of eval i get another error "ValueError: could not convert string to float:"
Is there anyone who can help me?
Posts: 8,160
Threads: 160
Joined: Sep 2016
Don't use eval, unless you know what you are doing. It's considered security risk as user can enter malicious code.
If you want them to be float, why do you use StringVar and not DoubleVar? DoubleVar will return float.
And what version of python do you use. If I am not mistaken and based on the error you get I think it's python2. It's recommend to move to python3, because python2 support ends 1.1.2020
Posts: 10
Threads: 2
Joined: Apr 2019
First of all, thank you very match for your quick reply.
I used eval because i want to do some math after this and actually i didn't know about DoubleVar, as i said im new in tkinter.
Now i have a new problem.
y = DoubleVar()
z = DoubleVar()
entry_1 = Entry(root, textvariable=y)
entry_1.grid(row=0, column=1)
entry_2 = Entry(root, textvariable=z)
entry_2.grid(row=1, column=1)
y = (y.get())
z = (z.get())
x = z / (y + 1)
m = z - x
k = ((z-x)/z)*100 i adjusted the code and now i got this error:
k = ((z-x)/z)*100
ZeroDivisionError: float division by zero
And the problem is that the error pops up before entering any data, just running the script.
And i also use python 3.6 :D
Posts: 8,160
Threads: 160
Joined: Sep 2016
Apr-11-2019, 09:06 AM
(This post was last modified: Apr-11-2019, 09:06 AM by buran.)
We cannot really give you advice, because you don't show your full code.
In any case you can refactor your code, so that any calculations are performed after you enter your data (e.g. require user to click a button to perform calculations) or if you don't want to click a button you can handle the error and/or set default value for the Var/Entry (e.g. 1.0, value will depend on your business case) and/or use if to check z value before performing calculations
Posts: 10
Threads: 2
Joined: Apr 2019
Here is my full code.
Just ignore the odd letter, it's just some comments in greek.
from tkinter import *
import tkinter.messagebox
import tkinter.simpledialog
root =Tk()
root.geometry("450x70")
root.title("Υπολογισμός ΦΠΑ")
root.configure(background='gray')
l1 = Label(root,text="Δώσε τιμή ΦΠΑ σε δεκαδική μορφη(π.χ. 0.24)",fg="black")
l1.grid(row=0, column=0, sticky=W)
l2 = Label(root,text="Δώσε αρχική τιμή προιόντος", fg="black")
l2.grid(row=1, column=0, sticky=W)
y = DoubleVar()
z = DoubleVar()
entry_1 = Entry(root, textvariable=y)
entry_1.grid(row=0, column=1)
entry_2 = Entry(root, textvariable=z)
entry_2.grid(row=1, column=1)
y = y.get()
z = z.get()
x = z / (y + 1) #καθαρή αξία προιόντος
m = z - x #τιμή ΦΠΑ
k = ((z-x) / z)*100 #ποσοστό διαφοράς
x= "{aaa:.2f}".format(aaa=x)
m="{aaa:.2f}".format(aaa=m)
k="{aaa:.2f}".format(aaa=k)
def print():
tkinter.messagebox.showinfo("Αποτέλεσμα", message =" ".join((
"Η καθαρή αξία του προιόντος είναι:", x, "€\n",
"Ο ΦΠΑ αντιστοιχεί σε:", m, "€\n",
"Το ποσοστό της έκπτωσης αν αφαιρεθεί ο ΦΠΑ είναι:", k,"%")))
button_1 = Button(root,text="Συνέχεια", command=print)
button_1.grid(columnspan=2)
root.mainloop()
Posts: 8,160
Threads: 160
Joined: Sep 2016
Apr-11-2019, 09:26 AM
(This post was last modified: Apr-11-2019, 09:27 AM by buran.)
with minimal changes
from tkinter import *
import tkinter.messagebox
import tkinter.simpledialog
root =Tk()
root.geometry("450x70")
root.title("Υπολογισμός ΦΠΑ")
root.configure(background='gray')
l1 = Label(root,text="Δώσε τιμή ΦΠΑ σε δεκαδική μορφη(π.χ. 0.24)",fg="black")
l1.grid(row=0, column=0, sticky=W)
l2 = Label(root,text="Δώσε αρχική τιμή προιόντος", fg="black")
l2.grid(row=1, column=0, sticky=W)
y_var = DoubleVar()
z_var = DoubleVar()
entry_1 = Entry(root, textvariable=y_var)
entry_1.grid(row=0, column=1)
entry_2 = Entry(root, textvariable=z_var)
entry_2.grid(row=1, column=1)
def calculate():
y = y_var.get()
z = z_var.get()
x = z / (y + 1) #καθαρή αξία προιόντος
m = z - x #τιμή ΦΠΑ
k = ((z-x) / z)*100 #ποσοστό διαφοράς
x= "{aaa:.2f}".format(aaa=x)
m="{aaa:.2f}".format(aaa=m)
k="{aaa:.2f}".format(aaa=k)
tkinter.messagebox.showinfo("Αποτέλεσμα", message =" ".join((
"Η καθαρή αξία του προιόντος είναι:", x, "€\n",
"Ο ΦΠΑ αντιστοιχεί σε:", m, "€\n",
"Το ποσοστό της έκπτωσης αν αφαιρεθεί ο ΦΠΑ είναι:", k,"%")))
button_1 = Button(root,text="Συνέχεια", command=calculate)
button_1.grid(columnspan=2) Note that this is not best as it uses global variables. Other things could be improved too
also don't use built-in functions (like print) for name of your own functions because you override the built-ins
Posts: 10
Threads: 2
Joined: Apr 2019
You are amazing, thank you so much!!
|