Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (anonymous):

I am trying to build a GUI calculation in Python but I keep getting this error message that "self" is not defined. Is anyone familiar with the work on this? Please assist. I will attach my code below.

OpenStudy (anonymous):

from tkinter import * root = Tk() class Calc(): def __init__(self, parent=None): 'calculator constructor' Frame.__init__(self, parent) self.pack() self.memory = '' # memory self.expr = '' # current expression self.startOfNextOperand = True def click(self, key): 'handler for event of pressing button labeled key' if key == '=': # evaluate the expression, including the value # displayed in entry and display result try: result = eval(self.expr + self.entry.get()) self.entry.delete(0, END) self.entry.insert(END, result) self.expr = '' except: self.entry.delete(0, END) self.entry.insert(END, 'Error') elif key in '+*-/': # add operand displayed in entry and operator key # to expression and prepare for next operand self.expr += self.entry.get() self.expr += key self.startOfNextOperand = True elif key == '\u221a': # compute and display square root of entry result = sqrt(eval(self.entry.get())) self.entry.delete(0, END) self.entry.insert(END, result) elif key == 'x\u00b2': # compute and display the square of entry result = eval(self.entry.get())**2 self.entry.delete(0, END) self.entry.insert(END, result) elif key == 'C': # clear entry self.entry.delete(0, END) elif key in {'M+', 'M-'}: # add or subtract entry value from memory self.memory = str(eval(self.memory+key[1]+self.entry.get())) elif key == 'MR': # replace value in entry with value stored in memory self.entry.delete(0, END) self.entry.insert(END, self.memory) elif key == 'MC': # clear memory self.memory = '' elif key == '+-': # switch entry from positive to negative or vice versa # if there is no value in entry, do nothing try: if self.entry.get()[0] == '-': self.entry.delete(0) else: self.entry.insert(0, '-') except IndexError: pass else: # insert digit at end of entry, or as the first # figit if start of next operand if self.startOfNextOperand: self.entry.delete(0, END) self.startOfNextOperand = False self.entry.insert(END, key) buttons = [['MC', 'M+', 'M-', 'MR'], ['C' , '\u221a', 'x\u00b2', '+' ], ['7' , '8' , '9' , '-' ], ['4' , '5' , '6' , '*' ], ['1' , '2' , '3' , '/' ], ['0' , '.' , '+-', '=' ]] # use Entry widget for display self.entry = Entry(self, relief=RIDGE, borderwidth=3, width=20, bg='gray', font=('Helvetica', 18)) self.entry.grid(row=0, column=0, columnspan=5) # create and place buttons in appropriate row and column for r in range(6): for c in range(4): # function cmd() is defined so that when it is # called without an input argument, it executes # self.click(buttons[r][c]) def cmd(x=buttons[r][c]): self.click(x) b = Button(self, # button for symbol buttons[r][c] text=buttons[r][c], width=3, relief=RAISED, command=cmd) # cmd() is the handler b.grid(row=r+1, column=c) # entry is in row 0 root.mainloop()

OpenStudy (rsmith6559):

I see a number of things that I'm suspicious of, but self not being defined I'd guess is because Calc is never instantiated.

OpenStudy (anonymous):

How can i do this? @rsmith6559

OpenStudy (rsmith6559):

It would probably need to be done in the main part of the program, before the mainloop. I don't know if this code is copied, or your own design, so I'm not sure where to put it.

OpenStudy (anonymous):

The spacing makes it a bit hard to debug but it looks like everything from the line "buttons = [['MC', 'M+', 'M-', 'MR']," on is outside the class definition. Using self will cause errors because self is not defined outside the scope of the class. I tried to copy and paste it into my editor but it all got put on one line. I know on some other sites they have special ways to post code. Not sure how it can be done here.

OpenStudy (rsmith6559):

I believe it's between triple back ticks (lowercase ~).

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!