在 TKINTER CALCULATOR 中添加一个 CLEAR ENTRY 按钮

Adding a CLEAR ENTRY button in TKINTER CALCULATOR

我正在用 Tkinter (Python) 编写一个计算器。我想添加一个 CLEAR ENTRY 按钮,用于清除 'entry' 用户提供的内容。不要与“全部清除”按钮混淆。

示例: 用户输入:- 35+87=62 然后按下 CE 按钮,所以他应该显示:- 35+87 如果他再次按下,他应该得到 35 等等直到 0.

我试了很多次,最后还是来找你了。 这是基本代码,(它没有制作任何CE按钮,您可以添加一个)。您可以使用正则表达式。

from tkinter import *
expression = "" 
def press(num): 
    global expression 
    expression = expression + str(num) 
    equation.set(expression) 
  
  

def equalpress(): 
    try: 
        global expression 
        total = str(eval(expression)) 
        equation.set(total) 
        expression = "" 
    except: 
        equation.set(" error ") 
        expression = "" 
def clear(): 
    global expression 
    expression = "" 
    equation.set("") 
  

if __name__ == "__main__": 
    # create a GUI window 
    gui = Tk() 
  
    # set the background colour of GUI window 
    gui.configure(background="light green") 
  
    # set the title of GUI window 
    gui.title("Simple Calculator") 
  
    # set the configuration of GUI window 
    gui.geometry("270x150") 
  
    # StringVar() is the variable class 
    # we create an instance of this class 
    equation = StringVar() 
  
    # create the text entry box for 
    # showing the expression . 
    expression_field = Entry(gui, textvariable=equation) 
  
    expression_field.grid(columnspan=4, ipadx=70) 
  
    equation.set('enter your expression') 
  
    button1 = Button(gui, text=' 1 ', fg='black', bg='red', 
                     command=lambda: press(1), height=1, width=7) 
    button1.grid(row=2, column=0) 
  
    button2 = Button(gui, text=' 2 ', fg='black', bg='red', 
                     command=lambda: press(2), height=1, width=7) 
    button2.grid(row=2, column=1) 
  
    button3 = Button(gui, text=' 3 ', fg='black', bg='red', 
                     command=lambda: press(3), height=1, width=7) 
    button3.grid(row=2, column=2) 
  
    button4 = Button(gui, text=' 4 ', fg='black', bg='red', 
                     command=lambda: press(4), height=1, width=7) 
    button4.grid(row=3, column=0) 
  
    button5 = Button(gui, text=' 5 ', fg='black', bg='red', 
                     command=lambda: press(5), height=1, width=7) 
    button5.grid(row=3, column=1) 
  
    button6 = Button(gui, text=' 6 ', fg='black', bg='red', 
                     command=lambda: press(6), height=1, width=7) 
    button6.grid(row=3, column=2) 
  
    button7 = Button(gui, text=' 7 ', fg='black', bg='red', 
                     command=lambda: press(7), height=1, width=7) 
    button7.grid(row=4, column=0) 
  
    button8 = Button(gui, text=' 8 ', fg='black', bg='red', 
                     command=lambda: press(8), height=1, width=7) 
    button8.grid(row=4, column=1) 
  
    button9 = Button(gui, text=' 9 ', fg='black', bg='red', 
                     command=lambda: press(9), height=1, width=7) 
    button9.grid(row=4, column=2) 
  
    button0 = Button(gui, text=' 0 ', fg='black', bg='red', 
                     command=lambda: press(0), height=1, width=7) 
    button0.grid(row=5, column=0) 
  
    plus = Button(gui, text=' + ', fg='black', bg='red', 
                  command=lambda: press("+"), height=1, width=7) 
    plus.grid(row=2, column=3) 
  
    minus = Button(gui, text=' - ', fg='black', bg='red', 
                   command=lambda: press("-"), height=1, width=7) 
    minus.grid(row=3, column=3) 
  
    multiply = Button(gui, text=' * ', fg='black', bg='red', 
                      command=lambda: press("*"), height=1, width=7) 
    multiply.grid(row=4, column=3) 
  
    divide = Button(gui, text=' / ', fg='black', bg='red', 
                    command=lambda: press("/"), height=1, width=7) 
    divide.grid(row=5, column=3) 
  
    equal = Button(gui, text=' = ', fg='black', bg='red', 
                   command=equalpress, height=1, width=7) 
    equal.grid(row=5, column=2) 
  
    clear = Button(gui, text='Clear', fg='black', bg='red', 
                   command=clear, height=1, width=7) 
    clear.grid(row=5, column='1') 
  
    Decimal= Button(gui, text='.', fg='black', bg='red', 
                    command=lambda: press('.'), height=1, width=7) 
    Decimal.grid(row=6, column=0) 
    gui.mainloop() 

在公式字段中,只允许使用数字、运算符和小数点。要清除最新条目,请从等式末尾搜索第一个运算符 (+ - * /),然后截断字符串。

将此函数用于 ClearEntry 按钮:

def clearentry(): 
    global expression 
    for i,c in enumerate(expression[::-1]): # reverse string
       if c in ['+','-','/','*']:  # find first operator 
           expression = expression[:-i-1]  # truncate string
           break
    else:
      expression = "" # no operator found
    equation.set(expression) 

我将此作为答案发布,因为我的声誉很低,无法发表任何评论。所以,我只是对 Mike67 的回答中的一个缺失点做了一个小补充。

如果在对方程求和后按清除输入按钮,它将显示方程。例如,如果您计算 2+3,而 5 在您的屏幕上。这将显示 2+3。我只是在这两个函数中添加了几行:

def equalpress():
    global backup_expression
    try:
        global expression
        total = str(eval(expression))
        equation.set(total)
        backup_expression = expression
        expression = ""

    except:
        equation.set(" error ")
        expression = ""

def clearentry():
    global expression,backup_expression
    for i,c in enumerate(expression[::-1]): # reverse string
       if c in ['+','-','/','*']:  # find first operator
           expression = expression[:-i-1]  # truncate string
           break
    else: # no operator found
        expression = backup_expression
    equation.set(expression)