from tkinter import *
from math import sqrt,pow
from tkinter import messagebox
Finestra=Tk()
class Applicazione(Frame):
def calcola(self):
self.x1field.delete("1.0",END)
self.x2field.delete("1.0",END)
try:
self.a=float(self.afield.get("1.0",END))
self.b=float(self.bfield.get("1.0",END))
self.c=float(self.cfield.get("1.0",END))
except:
messagebox.showinfo(title="Errore",message="Riempire tutti i campi!")
try:
self.x1=(-self.b-sqrt(pow(self.b,2)-4*self.a*self.c))/(2*self.a)
self.x1field.insert("1.0",str(self.x1))
except:
self.x1field.insert("1.0","Imp. in R")
try:
self.x2=(-self.b+sqrt(pow(self.b,2)-4*self.a*self.c))/(2*self.a)
self.x2field.insert("1.0",str(self.x2))
except:
self.x2field.insert("1.0","Imp. in R")
def widgets(self):
self.afield=Text(self, height=1, width=3)
self.afield.grid(row=0, column=0)
self.x2label=Label(self, text="x²+")
self.x2label.grid(row=0, column=1)
self.bfield=Text(self, height=1, width=3)
self.bfield.grid(row=0, column=2)
self.xlabel=Label(self, text="x+")
self.xlabel.grid(row=0, column=3)
self.cfield=Text(self, height=1, width=3)
self.cfield.grid(row=0, column=4)
self.label1=Label(self, text="=0 ")
self.label1.grid(row=0, column=5)
self.calcolabutton=Button(self, text="Calcola x1 e x2", command=self.calcola)
self.calcolabutton.grid(row=0, column=6)
self.x1label=Label(self, text=" x1=")
self.x1label.grid(row=0, column=7)
self.x1field=Text(self, height=1, width=10)
self.x1field.grid(row=0, column=8)
self.x2label=Label(self, text=" x2=")
self.x2label.grid(row=1, column=7)
self.x2field=Text(self, height=1, width=10)
self.x2field.grid(row=1, column=8)
def __init__(self,master=None):
Frame.__init__(self,master)
self.pack()
self.widgets()
app=Applicazione(master=Finestra)
Finestra.title("Equazioni di secondo grado")
Finestra.resizable(False,False)
app.mainloop()