简体   繁体   English

tkinter按钮按下可调用功能

[英]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). 无论如何试图用tkinter做一个科学的计算器,而我对此不是很好(并且python是我的第二个正确的任务)。 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 +. 我通过按钮调用它,但是我想通过do函数+。 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. 一般来说,类中的每个方法都应将self作为其第一个参数。 The name self is just a convention. self这个名字只是一个约定。 It is not a keyword in Python. 它不是Python中的关键字。 However, when you call a method such as obj.add(...) , the first argument sent to the method is the instance obj . 但是,当您调用诸如obj.add(...)之类的方法时,发送给该方法的第一个参数是实例obj It is a convention to call that instance self in the method definition. 约定在方法定义中调用该实例self So all your methods need to be modified to include self as the first argument: 因此,您需要修改所有方法以将self作为第一个参数:

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 "+" . 请注意,当您在方法do内调用self.do("+")X将绑定到"+" Later on in that method I see 后来用这种方法我看到

X=store[0+i]

which will rebind X to the value store[i] . 它将重新绑定X到值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. 我不知道您要在这里做什么,但是请注意,这样做意味着您刚刚丢失了刚刚传入的"+"值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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