我制作的 Python Tkinter 计算器无法正常工作

This Python Tkinter calculator I made isn't working properly

我制作的这个 Tkinter 计算器无法正常工作。

这是它的代码:

from tkinter import *

root = Tk()
root.geometry("350x200")
root.title("Calculator")

e1 = Entry(root, width=35, borderwidth=5)
e1.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
e1.insert(0, "0")

e2 = Entry(root, width=35, borderwidth=5)
e2.grid(row=1, column=0, columnspan=3, padx=10, pady=10)
e2.insert(0, "0")

First_number = float(e1.get())
Second_number = float(e2.get())

add = Button(root, text="Add", padx=10, pady=10, command=lambda: add(First_number, Second_number))
add.grid(row=3, column=0)

sub = Button(root, text="Subtract", padx=10, pady=10, command=lambda: sub(First_number, Second_number))
sub.grid(row=3, column=1)

mul = Button(root, text="Multiply", padx=10, pady=10, command=lambda: mul(First_number, Second_number))
mul.grid(row=3, column=2)

div = Button(root, text="Divide", padx=10, pady=10, command=lambda: div(First_number, Second_number))
div.grid(row=3, column=3)

e3 = Entry(root, width=35, borderwidth=5, font=("Arial", 10))
e3.grid(row=4, column=0, columnspan=3, padx=10, pady=20)
e3.insert(0, "0")

def add(First_number, Second_number):
    e3.delete(0, END)
    e3.insert(0, First_number + Second_number)

def sub(First_number, Second_number):
    e3.delete(0, END)
    e3.insert(0, First_number - Second_number)

def mul(First_number, Second_number):
    e3.delete(0, END)
    e3.insert(0, First_number * Second_number)

def div(First_number, Second_number):
    e3.delete(0, END)
    e3.insert(0, First_number / Second_number)            

root.mainloop()

当程序是 运行 时,小部件正确显示并且输入框工作,但是在我输入第一个和第二个数字并单击一个运算(加、减、乘或除)之后答案输入框显示0.0

我该怎么办?

这是因为您在 entry 中输入值之前设置了 First_numberSecond_number 的值 entry 的默认值是 0,它被设置为 First_numberSecond_number 值。然后你在所有情况下得到 0,在 division.

中得到 ZeroDivisionError

还有一个建议是你的功能和按钮名称是一样的。这不是一个好习惯。您可以将名称 add 更改为 add_btn,将 sub 更改为 sub_btn

这是格式化后的代码。

from tkinter import *


def add():

    First_number = float(e1.get())
    Second_number = float(e2.get())
    e3.delete(0, END)
    e3.insert(0, First_number + Second_number)

def sub():

    First_number = float(e1.get())
    Second_number = float(e2.get())
    e3.delete(0, END)
    e3.insert(0, First_number - Second_number)

def mul():

    First_number = float(e1.get())
    Second_number = float(e2.get())
    e3.delete(0, END)
    e3.insert(0, First_number * Second_number)

def div():

    First_number = float(e1.get())
    Second_number = float(e2.get())
    e3.delete(0, END)
    e3.insert(0, First_number / Second_number)            

root = Tk()
root.geometry("350x200")
root.title("Calculator")

e1 = Entry(root, width=35, borderwidth=5)
e1.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
e1.insert(0, "0")

e2 = Entry(root, width=35, borderwidth=5)
e2.grid(row=1, column=0, columnspan=3, padx=10, pady=10)
e2.insert(0, "0")


add_btn = Button(root, text="Add", padx=10, pady=10, command=add)
add_btn.grid(row=3, column=0)

sub_btn = Button(root, text="Subtract", padx=10, pady=10, command=sub)
sub_btn.grid(row=3, column=1)

mul_btn = Button(root, text="Multiply", padx=10, pady=10, command=mul)
mul_btn.grid(row=3, column=2)

div_btn = Button(root, text="Divide", padx=10, pady=10, command=div)
div_btn.grid(row=3, column=3)

e3 = Entry(root, width=35, borderwidth=5, font=("Arial", 10))
e3.grid(row=4, column=0, columnspan=3, padx=10, pady=20)
e3.insert(0, "0")

root.mainloop()

另一种方法是使用 global。我建议你使用上面的案例,因为使用 global 关键字不是一个好的做法。

from tkinter import *
First_number = 0
Second_number = 0
def first_second():
    global First_number,Second_number
    First_number = float(e1.get())
    Second_number = float(e2.get())
def add():
    e3.delete(0, END)
    e3.insert(0, First_number + Second_number)

def sub():
    e3.delete(0, END)
    e3.insert(0, First_number - Second_number)

def mul():
    e3.delete(0, END)
    e3.insert(0, First_number * Second_number)

def div():
    e3.delete(0, END)
    e3.insert(0, First_number / Second_number)            

root = Tk()
root.geometry("350x200")
root.title("Calculator")

e1 = Entry(root, width=35, borderwidth=5)
e1.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
e1.insert(0, "0")

e2 = Entry(root, width=35, borderwidth=5)
e2.grid(row=1, column=0, columnspan=3, padx=10, pady=10)
e2.insert(0, "0")


add_btn = Button(root, text="Add", padx=10, pady=10, command=lambda :[first_second(),add()])
add_btn.grid(row=3, column=0)

sub_btn = Button(root, text="Subtract", padx=10, pady=10, command=lambda :[first_second(),sub()])
sub_btn.grid(row=3, column=1)

mul_btn = Button(root, text="Multiply", padx=10, pady=10, command=lambda :[first_second(),mul()])
mul_btn.grid(row=3, column=2)

div_btn = Button(root, text="Divide", padx=10, pady=10, command=lambda :[first_second(),div()])
div_btn.grid(row=3, column=3)

e3 = Entry(root, width=35, borderwidth=5, font=("Arial", 10))
e3.grid(row=4, column=0, columnspan=3, padx=10, pady=20)
e3.insert(0, "0")