十进制按钮



hi人,所以我试图实现一个十进制按钮到一个计算器,我试图使用tkinter。小数按钮工作,但在进行计算时,它不考虑小数之后的任何内容,并且在我点击相等按钮后,它保留最近的小数。我也不知道为什么这是我现在的代码:

from tkinter import *
root = Tk()
root.title("Calculator")
e = Entry(root, width=35, borderwidth=5)
e.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
def button_press(number):
current = e.get()
e.delete(0, END)
e.insert(0, str(current) + str(number))
def button_clear():
e.delete(0, END)
def button_equal():
second_number = e.get()
e.delete(0, END)
if math == "addition":
e.insert(0, f_num + float(second_number))

if math == "subtraction":
e.insert(0, f_num - float(second_number))

if math == "multiplication":
e.insert(0, f_num * float(second_number))

if math == "division":
e.insert(0, f_num / float(second_number))
if math == "modulation":
e.insert(0, f_num % int(float(second_number)))

def button_add():
first_number = e.get()
global f_num
global math
math = "addition"
f_num = int(float(first_number))
e.delete(0, END)
def button_subtract():
first_number = e.get()
global f_num
global math
math = "subtraction"
f_num = int(float(first_number))
e.delete(0, END)
def button_multiply():
first_number = e.get()
global f_num
global math
math = "multiplication"
f_num = int(float(first_number))
e.delete(0, END)
def button_divide():
first_number = e.get()
global f_num
global math
math = "division"
f_num = int(float(first_number))
e.delete(0, END)
def button_modulo():
first_number = e.get()
global f_num
global math
math = "modulation"
f_num = int(float(first_number))
e.delete(0, END)
def button_num(number):
current=e.get()
e.delete(0, END)
e.insert(0,str(current)+ str(number))
def button_decimal():
current = e.get()
e.delete(0, END)
e.insert(0, str(current) + str("."))

#Number buttons
button_1 = Button(root, text="1", padx=25, pady=20, command=lambda: button_press(1))
button_2 = Button(root, text="2", padx=25, pady=20, command=lambda: button_press(2))
button_3 = Button(root, text="3", padx=25, pady=20, command=lambda: button_press(3))
button_4 = Button(root, text="4", padx=25, pady=20, command=lambda: button_press(4))
button_5 = Button(root, text="5", padx=25, pady=20, command=lambda: button_press(5))
button_6 = Button(root, text="6", padx=25, pady=20, command=lambda: button_press(6))
button_7 = Button(root, text="7", padx=25, pady=20, command=lambda: button_press(7))
button_8 = Button(root, text="8", padx=25, pady=20, command=lambda: button_press(8))
button_9 = Button(root, text="9", padx=25, pady=20, command=lambda: button_press(9))
button_0 = Button(root, text="0", padx=25, pady=20, command=lambda: button_press(0))
#Function buttons
button_div = Button(root, text="/", padx=26, pady=20, command=button_divide)
button_mult = Button(root, text="*", padx=26, pady=20, command=button_multiply)
button_sub = Button(root, text="-", padx=26, pady=20, command=button_subtract)
button_C = Button(root, text="C", padx=24, pady=20, command=lambda: button_clear())
button_equal = Button(root, text="=", padx=24, pady=20, command=button_equal)
button_add = Button(root, text="+", padx=25, pady=20, command=button_add)
button_dec = Button(root, text=u'u002E', padx=26, pady=20, command=button_decimal)
#button_lparen = Button(root, text="(", padx=26, pady=20, command=button_lparen)
#button_rparen = Button(root, text=")", padx=26, pady=20, command=button_rparen)
button_mod = Button(root, text="%", padx=24, pady=20, command=button_modulo)
#Frame
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_sub.grid(row=3, column=3)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_mult.grid(row=2, column=3)
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_div.grid(row=1, column=3)
button_0.grid(row=4, column=0)
button_C.grid(row=4, column=1)
button_equal.grid(row=4, column=2)
button_add.grid(row=4, column=3)
button_dec.grid(row=5, column=0)
#button_lparen.grid(row=5, column=1)
#button_rparen.grid(row=5, column=2)
button_mod.grid(row=5, column=3)

我很确定这是因为你将任何浮点数(小数)转换为整数,这意味着python将忽略小数点后的任何数字。

最新更新