【问题标题】:Adding a CLEAR ENTRY button in TKINTER CALCULATOR在 TKINTER CALCULATOR 中添加 CLEAR ENTRY 按钮
【发布时间】:2020-09-01 18:52:59
【问题描述】:

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

示例: 用户输入:- 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() 

【问题讨论】:

    标签: python python-3.x tkinter calculator tkinter-entry


    【解决方案1】:

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

    将此函数用于 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) 
    

    【讨论】:

      【解决方案2】:

      我将其发布为答案,因为由于我的声誉低,我无法发表任何评论。所以,我只是对 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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-06
        • 1970-01-01
        • 2011-01-16
        • 2022-01-11
        • 1970-01-01
        • 2018-11-05
        • 2017-12-02
        • 1970-01-01
        相关资源
        最近更新 更多