【问题标题】:How to keybind the return to the "=" code: Python如何将返回键绑定到“=”代码:Python
【发布时间】:2016-10-09 12:15:56
【问题描述】:

我正在尝试将输入键绑定到“=”。

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

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'

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

我在按 Enter 时收到该错误,当我运行代码时还会打开一个随机窗口,如果这有助于任何人帮助尝试解决问题,请帮助我,我真的被卡住了。

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

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()

顺便说一句,KEYBIND 在我的代码的底部

【问题讨论】:

  • 在此处直接以​​文本形式发布错误。不要发布文字图片。
  • 那么你需要解释什么不起作用。我看了你的照片,几乎没有帮助。说明准确您预计会发生什么,以及准确出了什么问题。如果您认为该图片确实是对您问题的最佳描述,则需要直接在此处发布。如果图像从其他站点中删除,您的问题将失去意义。无论如何,你需要解释一下图片中的行为是错误的。
  • 好吧这次我说的很清楚了
  • 查看下面的答案,请标记为已接受的答案。

标签: python user-interface tkinter calculator


【解决方案1】:

这应该可行。我也去掉了不需要的第二个窗口

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 2015-11-27
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多