Hi there in my code I'm am getting an the error AttributeError: 'GUI' object has no attribute 'calculate_button' And I don't know why its not working when I am using two classes. Also I don't want to make the class inherit each other nor make it an object.
Here is my code:
from tkinter import *
from tkinter import ttk
class Logic:
def __init__(self,a,b,c,d):
self.a = a
self.b = b
self.c = c
self.d = d
as_meter = dict(mm=0.001, cm=0.01, inch=0.0254,ft=0.3048, yd=0.9144, m=1.0,km=1000.0, mi=1609.344,)
def calculate(self, *args):
try:
v = float(c)
except ValueError:
self.d.set('Invalid Input')
return
m = v * sign * self.as_meter [self.a]
r = m/self.as_meter[self.b]
answer = (self.d.set("{:.5g}".format(r)))
return answer
class GUI:
def __init__(self,root):
self.notebook = ttk.Notebook(root)
self.notebook.pack()
self.length_frame = ttk.Frame(self.notebook)
self.notebook.add(self.length_frame, text = 'Length')
#Combobox
self.measurements = StringVar()
self.Combobox_Length_Left = ttk.Combobox(self.length_frame, textvariable = self.measurements, values = ('mm', 'cm', 'inch', 'ft', 'yd', 'm', 'km', 'mi'), width = 10,state=['readonly'])
self.Combobox_Length_Left.current(5)
self.Combobox_Length_Left.grid(row = 2, column = 0, padx = 5, pady = 5, sticky = E)
self.measurements1 = StringVar()
self.Combobox_Length_Right = ttk.Combobox(self.length_frame, textvariable = self.measurements1, value = ('mm', 'cm', 'inch', 'ft', 'yd', 'm', 'km', 'mi'), width = 10,state = ['readonly'])
self.Combobox_Length_Right.current(5)
self.Combobox_Length_Right.grid(row = 2, column = 2, padx = 5, pady = 5, sticky = E)
#Labels
self.Conversion = ttk.Label(self.length_frame, text = 'Convertion:').grid(row = 1, column = 0, padx = 5, pady = 5, sticky = W)
self.Label_Blank = ttk.Label(self.length_frame, text = '').grid(row = 1, column = 1, padx = 5, pady = 5, sticky = E)
self.Label_To2 = ttk.Label(self.length_frame, text = 'to').grid(row = 2, column = 1, padx = 5, pady = 5, sticky = E)
self.Label_To = ttk.Label(self.length_frame, text = 'to').grid(row = 3, column = 1, padx = 5, pady = 5, sticky = E)
#Entry Boxes
self.Text_Length_Left = StringVar()
self.Entry_Length_Left = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Left,width = 13)
self.Entry_Length_Left.grid(row = 3, column = 0, padx = 5, pady = 5)
self.Text_Length_Right = StringVar()
self.Entry_Length_Right = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Right,width = 13, state='readonly')
self.Entry_Length_Right.grid(row = 3, column = 2, padx = 5, pady = 5)
def calculate_button(self):
c_l = self.measurements.get()
c_r = self.measurements1.get()
e_l = self.Text_Length_Left.get()
e_r = self.Text_Length_Right.get()
c = Logic(c_l,c_r,e_l,e_r)
self.Text_Length_Right.set('')
self.Text_Length_Right.set(c.calulate())
#Button
self.Convert_Button = ttk.Button(self.length_frame, text = 'Convert',command = calculate_button, width = 10).grid(row = 1, column = 2, sticky = S)
def main():
root = Tk()
app = GUI(root)
root.title('Metric Calculator')
root.resizable(False,False)
root.mainloop()
main()
Thanks for any help!
def calculate_button(self):is indented into the__init__method instead of the class, just de-dent the code so it is in the right spot.