在Python中修改计算器的代码

oxiaedzo  于 2023-04-10  发布在  Python
关注(0)|答案(2)|浏览(151)

我有一个问题,当我已经执行了两个数字之间的操作,而不是一个新的操作,我按下一个数字,数字坚持的结果,这里是一个例子-我输入5+5的结果是10,但然后当我点击一个数字按钮,例如5它变成105,但是我想清除结果并开始新的操作。有人能帮忙吗?提前感谢!下面是代码:

import tkinter
from tkinter import *
from tkinter import messagebox
import math   
def button_1_is_Clicked():  
    global var
    var = var + "1"  
    the_data.set(var)
def button_2_is_Clicked():  
    global var  
    var = var + "2"  
    the_data.set(var)    
def button_3_is_Clicked():  
    global var  
    var = var + "3"  
    the_data.set(var)  
def button_4_is_Clicked():  
    global var  
    var = var + "4"  
    the_data.set(var)   
def button_5_is_Clicked():  
    global var  
    var = var + "5"  
    the_data.set(var)   
def button_6_is_Clicked():  
    global var  
    var = var + "6"  
    the_data.set(var)  
def button_7_is_Clicked():  
    global var  
    var = var + "7"  
    the_data.set(var)    
def button_8_is_Clicked():  
    global var  
    var = var + "8"  
    the_data.set(var)   
def button_9_is_Clicked():  
    global var  
    var = var + "9"  
    the_data.set(var)   
def button_0_is_Clicked():  
    global var  
    var = var + "0"  
    the_data.set(var)   
def button_Add_is_Clicked():  
    global A  
    global var  
    global operator
    global prevous_operator
    A = float(var)  
    operator = "+"  
    var = var + "+"
    the_data.set(var)
def button_Sub_is_Clicked():  
    global A  
    global var  
    global operator
    global prevous_operator
    A = float(var)  
    operator = "-"  
    var = var + "-"  
    the_data.set(var)
def button_Mul_is_Clicked():  
    global A  
    global var  
    global operator
    global prevous_operator
    A = float(var)  
    operator = "*"  
    var = var + "*"  
    the_data.set(var)
def button_Div_is_Clicked():  
    global A  
    global var  
    global operator
    global prevous_operator
    A = float(var)  
    operator = "/"  
    var = var + "/"  
    the_data.set(var)
def button_Equal_is_Clicked():  
    global A  
    global var  
    global operator  
    A = float(var)  
    operator = "="  
    var = var + "="  
    the_data.set(var)
def button_Sqrt_is_Clicked():
    global A
    global var
    global operator
    global prevous_operator
    A=float(var)
    operator="sqrt"
    var="√"+var
    the_data.set(var)
def button_Square_is_Clicked():
    global A
    global var
    global operator
    A=float(var)
    operator="square"
    var=var+"*"+var
    the_data.set(var)
def button_Percent_is_Clicked():  
    global A  
    global var  
    global operator
    global prevous_operator
    A = float(var)  
    operator = "%"  
    var = var + "%"  
    the_data.set(var)
def button_Inv_is_Clicked():  
    global A  
    global var  
    global operator
    global prevous_operator
    A = float(var)  
    operator = "1/x"  
    var = "1/"+var  
    the_data.set(var)
    var = ""
def button_C_is_Clicked():  
    global A  
    global var  
    global operator  
    var = ""  
    A = 0  
    operator = ""  
    the_data.set("0")

def button_PlusMinus_is_Clicked():  
    global A  
    global var  
    global operator  
    A = float(var)  
    operator = "+/-"  
    var = var + "+/-"  
    the_data.set(var) 

def button_MC_is_Clicked():  
    global A  
    global var  
    global operator  
    operator = "MC"  
    the_data.set("") 

def button_MR_is_Clicked():  
    global A  
    global var  
    global operator  
    operator = "MR"  
    the_data.set("") 

def button_MS_is_Clicked():  
    global A  
    global var  
    global operator  
    operator = "MS"  
    the_data.set("") 

def button_MPlus_is_Clicked():  
    global A  
    global var  
    global operator  
    operator = "M+"  
    the_data.set("") 

def button_MMinus_is_Clicked():  
    global A  
    global var  
    global operator
    operator = "M-"  
    the_data.set("")

def button_CE_is_Clicked():
    global var
    var = var[:-1]
    if var == "":
        the_data.set("0")
    else:
        the_data.set(var)

    

var = ""
A = 0
operator = ""
memory = 0

def res():
    global A
    global operator
    global var
    global memory
    var2 = var
    try:
        if operator == "+":
            a = float((var2.split("+")[1]))
            A += a
            the_data.set(A)
            var = str(A)
        elif operator == "-":
            a = float((var2.split("-")[1]))
            A -= a
            the_data.set(A)
            var = str(A)
            var = ""
        elif operator == "*":
            a = float((var2.split("*")[1]))
            A *= a
            the_data.set(A)
            var = str(A)
            var = ""
        elif operator == "/":
            a = float((var2.split("/")[1]))
            if a == 0:
                raise ZeroDivisionError
            else:
                A /= a
                the_data.set(A)
                var = str(A)
                var = ""
        elif operator == "sqrt":
            if A < 0:
                raise ValueError("Cannot take square root of a negative number.")
            A = math.sqrt(A)
            the_data.set(A)
            var = str(A)
            var = ""
        elif operator == "square":
            a=float((var2.split("*")[1]))
            A*=A
            the_data.set(A)
            var = str(A)
            var = ""
        elif operator == "%":
            a = float(var2.split("%")[0])
            A *= (a/100)
            the_data.set(A)
            var = str(A)
            var = ""
        elif operator == "1/x":
            if A == 0:
                raise ZeroDivisionError("Cannot divide by zero.")
            A = 1/A
            the_data.set(A)
            var = str(A)
            var = ""
        elif operator == "+/-":
            A = -A
            the_data.set(A)
            var = str(A)
            var = ""
        elif operator == "MC":
            memory = 0
        elif operator == "MR":
            A = memory
            the_data.set(A)
        elif operator == "MS":
            memory = A
        elif operator == "M+":
            memory += A
        elif operator == "M-":
            memory -= A
        elif operator == "CE":
            if len(var) > 1:
                var = var[:-1]
                A = float(var)
                the_data.set(A)
            else:
                var = ""
                A = 0
                the_data.set(0)
       

    except ZeroDivisionError as e:
        messagebox.showerror("Error", str(e))
        var = ""
    except ValueError as e:
        messagebox.showerror("Error", str(e))
        var = ""

我什么都试过了

r8uurelv

r8uurelv1#

如果不想更改脚本体系结构(我认为,你需要这样做!),你将需要清除_data后,你已经显示结果。我认为,你需要一个变量(但不建议设置global),其中设置了displayed_result,如果按number且displayed_result为true,则需要清除_data中的所有,但如果按operator,你需要清除all before equal(类似于:int n = var.split();the_data.set(n if len(n)〈2 else n[1]))并将n[1]设置为A。

l7wslrjt

l7wslrjt2#

为了解决这个问题,我们可以添加一个名为clear_var的函数,它将var变量设置为空字符串。然后我们可以在启动新操作时调用这个函数,例如单击操作符按钮或等号按钮。
以下是如何将此函数添加到代码中的示例:

def clear_var():
    global var
    var = ""
    the_data.set(var)

def button_Add_is_Clicked():
    global A
    global var
    global operator
    global prevous_operator
    A = float(var)
    operator = "+"
    var = var + "+"
    clear_var()

def button_Sub_is_Clicked():
    global A
    global var
    global operator
    global prevous_operator
    A = float(var)
    operator = "-"
    var = var + "-"
    clear_var()

# ... and so on for the other operator buttons

def button_Equal_is_Clicked():
    global A
    global var
    global operator
    global previous_operator
    B = float(var.split(operator)[1])
    if operator == "+":
        result = A + B
    elif operator == "-":
        result = A - B
    elif operator == "*":
        result = A * B
    else:
        result = A / B
    var = str(result)
    the_data.set(var)
    clear_var()

在本例中,clear_varvar变量设置为空字符串,并更新标签以显示var的新值。然后,我们在每个operator函数的末尾调用此函数以清除变量并开始新的操作。

相关问题