我试图在计算器中添加大量数字,但我仍然无法获得所需的总和,仅是最后两个数字的总和

问题描述 投票:2回答:1

因此,我从freecodecamp.org开始了tkinter教程,现在我正要制作一个计算器。我会按照本教程进行操作,同时还要自己做一些实验。这里出现了我的问题,在视频中,计算器输出了最后两个数字的和,如果您按“数字+数字=”,那么无论您按多少个数字,然后按+,只有最后两个数字被添加。我想做的是,每当我按下+按钮时,总和就会存储在全局变量中,但是我很难这样做。

from tkinter import *

root = Tk()
root.title("Simple calculator")

e = Entry(root, width=35, borderwidth=3)
e.grid(row=0, column=0, columnspan=3, padx=10, pady=10)


# button functions

def button_click(number):
    # e.delete(0, END)
    current = e.get()
    e.delete(0, END)
    e.insert(0, str(current) + str(number))


def button_clear():
    e.delete(0, END)


def button_add():
    temp = 0 + int(e.get())
    global v_keeper
    v_keeper = int(temp)
    e.delete(0,END)


def button_equal():
    temp = int(e.get())
    e.delete(0,END)
    e.insert(0,v_keeper + temp)


#button gui
button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0))
button_add = Button(root, text="+", padx=39, pady=20, command=button_add)
button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal)
button_clear = Button(root, text="Clear", padx=79, pady=20, command=button_clear)

# put buttons on screen

button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)

button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)

button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)

button_0.grid(row=4, column=0)
button_clear.grid(row=4, column=1, columnspan=2)
button_add.grid(row=5, column=0)
button_equal.grid(row=5, column=1, columnspan=2)

# myButton = Button(root, text="Enter your name", command=myClick)


root.mainloop()
python function button tkinter
1个回答
1
投票

免责声明:您应尽可能避免使用全局变量。它们往往会导致无法维护的,非结构化的代码。

这是您的全局变量的解决方案:

首先:在脚本的开头定义全局变量v_keep,并将其初始化为0。

第二:在button_add中,您为其分配了错误的值。您想要将当前的总和存储在那里。

第三:您还需要在v_keeperbutton_clear中更新button_equal

这是我更改的代码部分,为了获得所需的结果:


v_keeper = 0


# button functions

def button_click(number):
    # e.delete(0, END)
    current = e.get()
    e.delete(0, END)
    e.insert(0, str(current) + str(number))


def button_clear():
    global v_keeper
    v_keeper = 0
    e.delete(0, END)


def button_add():
    global v_keeper
    v_keeper = v_keeper + int(e.get())
    e.delete(0, END)

更新button_equal作为练习留给读者:)

© www.soinside.com 2019 - 2024. All rights reserved.