0

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!

3
  • In the self.convert_button, remove the parenthesis from self.calculate_button(). Keep it just self.calculate_button Commented Jun 5, 2016 at 5:04
  • Yeah I have tried that and I still get the same error. Commented Jun 5, 2016 at 5:05
  • 1
    the 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. Commented Jun 5, 2016 at 5:13

2 Answers 2

2

There are few issues with your code:

  1. As @Tadhg McDonald-Jensen commented you, calculate_button() is wrongly intended and does not belong to GUI() class
  2. As @MotKohn answered, you must remove () from calculate_button() in Convert_Button button.
  3. Logic() is not defined neither in GUI() nor in Converversion() classes.
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help man! Really appreciate it! But now im getting the error! TypeError: calculate_button() missing 5 required positional arguments: 'self', 'c_l', 'c_r', 'e_l', and 'e_r'
What is sign in calculate()? It is not defined anywhere.
don't you mean self
No, I mean sign in this line of code: m = v * sign * self.as_meter [self.a]
Hi even if you remove sign I still get the error TypeError: calculate_button() missing 5 required positional arguments: 'self', 'c_l', 'c_r', 'e_l', and 'e_r'
1

unindent calculate_button 1 level. It should not be a function in the init function rather a function of the class GUI. Remove () as mentioned in comment.

There is still a bug,

NameError: name 'Logic' is not defined

1 Comment

Yeah I have done that but I got the error UnboundLocalError: local variable 'calculate_button' referenced before assignment here is a picture gyazo.com/a2785666ce624ff85f8e4f1cdafb39c9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.