简体   繁体   中英

tkinter button press to function call

hey guys first post and what not so hi. anyway trying to make a scientific calculator with tkinter and im not very good with it(and python its my second proper assignment). anyway most of the code will probably be wrong but im just trying to take it one step at a time in particular im concerned about the function add. im calling it via the button however i want to pass the do function a +. this in turn creating an array i can calculate from. it keeps erroring and i dont know how to fix it. its really annoying me now so if someone could help out would be much appreciated

from tkinter import*
from operator import*

class App:
    def __init__(self,master):#is the master for the button widgets
        frame=Frame(master)
        frame.pack()
        self.addition = Button(frame, text="+", command=self.add)#when clicked sends a call back for a +
        self.addition.pack()
    def add(Y):
        do("+")
    def do(X):#will hopefully colaborate all of the inputs
        cont, i = True, 0
        store=["+","1","+","2","3","4"]
        for i 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 the array to be split to find the 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 a class should take self as its first argument. The name self is just a convention. It is not a keyword in Python. However, when you call a method such as obj.add(...) , the first argument sent to the method is the instance obj . It is a convention to call that instance self in the method definition. So all your methods need to be modified to include self as the first argument:

class App:
    def __init__(self, master):#is the master for the button widgets
        frame=Frame(master)
        frame.pack()
        self.addition = Button(frame, text="+", command=self.add)#when clicked sends a call back for a +
        self.addition.pack()
    def add(self):
        self.do("+")
    def do(self, X):
        ...

Note that when you call self.do("+") , inside the method do , X will be bound to "+" . Later on in that method I see

X=store[0+i]

which will rebind X to the value store[i] . I don't know what you are trying to do here, but be aware that doing so means you've just lost the "+" value that was just passed in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM