Advertisement
yohanga

QuadrantEquationSolverGUI

Feb 26th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. __author__ = "Dmitriy Krasota aka g0t0wasd"
  4.  
  5. # An example of Quadratic Calc using Tkinter.              
  6. # More at http://pythonicway.com/index.php/python-examples/python-gui-examples/14-python-tkinter-quadratic-equations
  7.  
  8.  
  9. from Tkinter import *
  10. from math import sqrt
  11.  
  12. def solver(a,b,c):
  13.     """ Solves quadratic equation and returns the result in formatted string """
  14.     D = b*b - 4*a*c
  15.     if D >= 0:
  16.         x1 = (-b + sqrt(D)) / (2*a)
  17.         x2 = (-b - sqrt(D)) / (2*a)
  18.         text = "The discriminant is: %s \n X1 is: %s \n X2 is: %s \n" % (D, x1, x2)        
  19.     else:
  20.         text = "The discriminant is: %s \n This equation has no solutions" % D
  21.     return text
  22.  
  23. def inserter(value):
  24.     """ Inserts specified value into text widget """
  25.     output.delete("0.0","end")
  26.     output.insert("0.0",value)    
  27.  
  28. def clear(event):
  29.     """ Clears entry form """
  30.     caller = event.widget
  31.     caller.delete("0", "end")
  32.  
  33. def handler():
  34.     """ Get the content of entries and passes result to the text """
  35.     try:
  36.         # make sure that we entered correct values
  37.         a_val = float(a.get())
  38.         b_val = float(b.get())
  39.         c_val = float(c.get())
  40.         inserter(solver(a_val, b_val, c_val))
  41.     except ValueError:
  42.         inserter("Make sure you entered 3 numbers")
  43.  
  44. root = Tk()
  45. root.title("Quadratic calculator")
  46. root.minsize(325,230)
  47. root.resizable(width=False, height=False)
  48.  
  49.  
  50. frame = Frame(root)
  51. frame.grid()
  52.  
  53. a = Entry(frame, width=3)
  54. a.grid(row=1,column=1,padx=(10,0))
  55. a.bind("<FocusIn>", clear)
  56. a_lab = Label(frame, text="x**2+").grid(row=1,column=2)
  57.  
  58. b = Entry(frame, width=3)
  59. b.bind("<FocusIn>", clear)
  60. b.grid(row=1,column=3)
  61. b_lab = Label(frame, text="x+").grid(row=1, column=4)
  62.  
  63. c = Entry(frame, width=3)
  64. c.bind("<FocusIn>", clear)
  65. c.grid(row=1, column=5)
  66. c_lab = Label(frame, text="= 0").grid(row=1, column=6)
  67.  
  68. but = Button(frame, text="Solve", command=handler).grid(row=1, column=7, padx=(10,0))
  69.  
  70. output = Text(frame, bg="lightblue", font="Arial 12", width=35, height=10)
  71. output.grid(row=2, columnspan=8)
  72.  
  73. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement