from Tkinter import *
from cmath import sqrt
window = Tk()
solution = "Nothing to see for now ~"
a = Entry(window)
a.pack()
b = Entry(window)
b.pack()
c = Entry(window)
c.pack()
result_text = Text(window)
result_text.pack()
def calculate():
na = int(a.get())
nb = int(b.get())
nc = int(c.get())
delta = (nb **2) - (4 * na * nc )
if delta > 0:
x1 = ((- nb) + sqrt(delta)) / (2 * na)
x2 = ((- nb) - sqrt(delta)) / (2 * na)
solution = "x1 = " + str(x1) + "\n x2 = " + str(x2)
return solution
elif delta==0:
x = (-nb) / (2 * na)
solution = "x1 = " + str(x)
return solution
else:
solution = "There isn't any solution for this equation."
return solution
button = Button(window , text = "Calculate" , command = calculate)
button.pack()
result_text.insert(END,solution)
mainloop()
I'm still a beginner btw ... so basically what I'm trying to do is to program a simple GUI with Tkinter that calculates the solutions for a quadratic equation ,the user just give the a ,b and c of the equation and the program shows the result in a text widget after clicking the "calculate" button ,I 've done things fine but it still don't want to refresh the text widget with the result instead of the "Nothing to see for now ~" message ! What should I do please?
Returning solution
doesn't do anything, because Tkinter simply discards the result of the function. You need to update the result text from within calculate
.
def calculate():
na = int(a.get())
nb = int(b.get())
nc = int(c.get())
delta = (nb **2) - (4 * na * nc )
if delta > 0:
x1 = ((- nb) + sqrt(delta)) / (2 * na)
x2 = ((- nb) - sqrt(delta)) / (2 * na)
solution = "x1 = " + str(x1) + "\n x2 = " + str(x2)
elif delta==0:
x = (-nb) / (2 * na)
solution = "x1 = " + str(x1) + "\n x2 = " + str(x2)
else:
solution = "There isn't any solution for this equation."
result_text.delete("1.0", END)
result_text.insert(END, solution)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.