如何键入返回“=”代码:Python

时间:2016-10-09 12:15:57

标签: python user-interface tkinter calculator

我正在尝试将回车键键入“=”。

使用我现在的代码,当我输入两个数字并按回车键时出现错误,错误是:

Exception in Tkinter callback
Traceback (most recent call last):
line 1550, in __call__
return self.func(*args)

line 68, in <lambda>
root.bind('<Return>', lambda x: Calculator.calcu('='))
TypeError: calcu() missing 1 required positional argument: 'entry_num'

随机窗口在开始时弹出的事实只是证明它不起作用我猜。我相信如果根窗口问题得到修复,它可以解决密钥绑定问题。

当我按回车键时出现错误,还有一个随机窗口打开,因为我运行代码也可以帮助任何人帮助尝试解决问题,请帮助我,我真的卡住了。

总体问题是: 我不知道如何将键盘上的回车键绑定到“=”并通过这样做来输入条目。

This is what I get when i run my code(picture)

    from __future__ import division
from functools import partial 
from math import * 
import tkinter as tk 
from tkinter import *  
root=Tk()
class Calculator(tk.Tk): 
def __init__(self):     
    tk.Tk.__init__(self)
    self.buttons_layout()       
    self.title("Thomas's calc") 
    textb = Entry(root)

def calcu(self,entry_num):

    if entry_num == "=":  
        try: 
            total=eval(self.textb.get()) 
            self.textb.insert(tk.END," = "+str(total)) 
        except: 
            self.textb.insert(tk.END, " - Invalid Calculation - ") 


    elif entry_num == "del":
        self.txt=self.textb.get()[:-1]
        self.textb.delete(0,tk.END)
        self.textb.insert(0,self.txt)

    elif entry_num == "C":
        self.textb.delete(0,END) 

    elif entry_num == "i":      
        infob= Toplevel(self.textb.insert(tk.END, ""))   
        infob = Label(infob, text="Thomas, Calculator").pack()  


    else: 
        if "=" in self.textb.get(): 
            self.textb.delete(tk.END)           
        self.textb.insert(tk.END, entry_num)

def buttons_layout(self): 

    self.textb = tk.Entry(root, width=66, fg="white", bg="black", state="normal") #text color = fg // background colour bg // sets the entry box specs
    self.textb.grid(row=0, column=0, columnspan=5) 

    buttona="groove"                       

    ycol = 0                         

    xrow = 1                                     

    button_list = ["1","2","3","+","C",
                   "4","5","6","-","del",       
                   "7","8","9","/","",
                   "0",".","i","*","="]

    for button in button_list:             
        calc1=partial(self.calcu, button) 
        tk.Button(root,text=button,height=4,command=calc1,bg="aquamarine", fg="red",width=10,state="normal",relief=buttona).grid(
            row=xrow, column=ycol)
        ycol= ycol + 1 
        if ycol > 4:  
            ycol=0  
            xrow= xrow + 3 

class key_binding():
    root.bind('<Return>', lambda x: Calculator.calcu('='))

end=Calculator()
end.mainloop()
root.mainloop()

键盘是以我的方式在我的代码的底部

1 个答案:

答案 0 :(得分:0)

这应该有效。我也摆脱了第二个不需要的窗口

from __future__ import division
from functools import partial 
from math import * 
import tkinter as tk 
from tkinter import *  
#root=Tk()
class Calculator(tk.Tk): 
    def __init__(self):     
        tk.Tk.__init__(self)
        self.buttons_layout()       
        self.title("Thomas's calc") 
        textb = Entry(self)
        self.bind('<Return>', lambda x: self.calcu('='))

    def calcu(self,entry_num):

        if entry_num == "=":  
            try: 
                total=eval(self.textb.get()) 
                self.textb.insert(tk.END," = "+str(total)) 
            except: 
                self.textb.insert(tk.END, " - Invalid Calculation - ") 


        elif entry_num == "del":
            self.txt=self.textb.get()[:-1]
            self.textb.delete(0,tk.END)
            self.textb.insert(0,self.txt)

        elif entry_num == "C":
            self.textb.delete(0,END) 

        elif entry_num == "i":      
            infob= Toplevel(self.textb.insert(tk.END, ""))   
            infob = Label(infob, text="Thomas, Calculator").pack()  


        else: 
            if "=" in self.textb.get(): 
                self.textb.delete(tk.END)           
            self.textb.insert(tk.END, entry_num)

    def buttons_layout(self): 

        self.textb = tk.Entry(self, width=66, fg="white", bg="black", state="normal") #text color = fg // background colour bg // sets the entry box specs
        self.textb.grid(row=0, column=0, columnspan=5) 

        buttona="groove"                       

        ycol = 0                         

        xrow = 1                                     

        button_list = ["1","2","3","+","C",
                       "4","5","6","-","del",       
                       "7","8","9","/","",
                       "0",".","i","*","="]

        for button in button_list:             
            calc1=partial(self.calcu, button) 
            tk.Button(self,text=button,height=4,command=calc1,bg="aquamarine", fg="red",width=10,state="normal",relief=buttona).grid(
                row=xrow, column=ycol)
            ycol= ycol + 1 
            if ycol > 4:  
                ycol=0  
                xrow= xrow + 3 

#class key_binding():
#    root.bind('<Return>', lambda x: Calculator.calcu('='))

end=Calculator()
end.mainloop()
#root.mainloop()