python - tkinter button press to function call -
hey guys first post , not hi. anyway trying make scientific calculator tkinter , im not it(and python second proper assignment). anyway of code wrong im trying take 1 step @ time in particular im concerned function add. im calling via button want pass function +. in turn creating array can calculate from. keeps erroring , dont know how fix it. annoying me if out appreciated
from tkinter import* operator import* class app: def __init__(self,master):#is master button widgets frame=frame(master) frame.pack() self.addition = button(frame, text="+", command=self.add)#when clicked sends call + self.addition.pack() def add(y): do("+") def do(x):#will colaborate of inputs cont, = true, 0 store=["+","1","+","2","3","4"] in range(5): x=store[0+i] print(store[0+i]) cont = false if cont == false: print(eval_binary_expr(*(store[:].split()))) def get_operator_fn(op):#allows array split find operators return { '+' : add, '-' : sub, '*' : mul, '/' : truediv, }[op] def eval_binary_expr(op1, num1, op2, num2): store[1],store[3] = int(num1), int(num2) return get_operator_fn(op2)(num1, num2) root=tk() app=app(root) root.mainloop()#runs programme
generally speaking, every method in class should take self
first argument. name self
convention. not keyword in python. however, when call method such obj.add(...)
, first argument sent method instance obj
. convention call instance self
in method definition. methods need modified include self
first argument:
class app: def __init__(self, master):#is master button widgets frame=frame(master) frame.pack() self.addition = button(frame, text="+", command=self.add)#when clicked sends call + self.addition.pack() def add(self): self.do("+") def do(self, x): ...
note when call self.do("+")
, inside method do
, x
bound "+"
. later on in method see
x=store[0+i]
which rebind x
value store[i]
. don't know trying here, aware doing means you've lost "+"
value passed in.
Comments
Post a Comment