Answer the question
In order to leave comments, you need to log in
How to fix 'NoneType' object has no attribute 'get' error?
When you run the code and click in the "Calculate" window, it gives an error - line 30, in calculate
a = self.a.get()
AttributeError: 'NoneType' object has no attribute 'get'
Can you please explain what is the problem?
Thanks in advance.
from tkinter import*
from math import sqrt
class App(Frame):
def __init__(self, master):
super(App, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
Label(self,text = 'Коэффициент a').grid(row = 0, column = 0)
self.a = Entry(self).grid(row = 0, column = 1, sticky = W)
Label(self,text = 'Коэффициент b').grid(row = 1, column = 0)
self.b = Entry(self).grid(row = 1, column = 1, sticky = W)
Label(self,text = 'Коэффициент c').grid(row = 2, column = 0)
self.c = Entry(self).grid(row = 2, column = 1, sticky = W)
Button(self, text = "Вычислить", command = self.calculate).grid(row = 3, column = 0)
Button(self, text = 'Выйти', command = self.quit).grid(row = 4, column = 0)
self.results = Text(self, width = 40, height = 5, wrap = WORD)
self.results.grid(row = 5, column = 0, columnspan = 2)
def calculate(self):
a = self.a.get()
b = self.b.get()
c = self.c.get()
D = b**2 - 4*a*c
if D < 0:
message = 'Нет решений'
elif D > 0:
x1 = (-b-sqrt(D))/(2*a)
x2 = (-b+sqrt(D))/(2*a)
message = 'x1 = ' + str(x1) + '\n'
message += 'x2 = ' + str(x2)
elif D == 0:
x = (-b)/(2*a)
message = 'x = '+ str(x)
self.results.delete(0,0, END)
self.results.insert(0,0, message)
root = Tk()
root.title('Квадратные уравнения')
root.geometry('350x250')
app = App(root)
root.mainloop()
Answer the question
In order to leave comments, you need to log in
Rewrite all widget declarations like this:
self.a = Entry(self)
self.a.grid(row = 0, column = 1, sticky = W)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question