Python / Tkinter-
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python / Tkinter-相关的知识,希望对你有一定的参考价值。
我正在开发一个用于计算帐单税的应用程序,同时,如果计算基础的值((receita1 * 32/100)+(receita2 * 8/100))的值大于60k,它将计算的盈余,((((receita1 * 32/100)+(receita2 * 8/100))-60000),但是出现以下错误:
v_result3.set(real(receita1 * 32 / 100) + (receita2 * 8 / 100))
TypeError: can only concatenate str (not "float") to str
这里是完整的代码:
from tkinter import *
root = Tk()
root.geometry('350x350')
def real(my_value):
a = '{:,.2f}'.format(float(my_value))
b = a.replace(',', 'v')
c = b.replace('.', ',')
return c.replace('v', '.')
l_label = Label(root, text='Receita 1')
l_label.place(x=10, y=10)
e_entry = Entry(root)
e_entry.place(x=100, y=10)
l_label2 = Label(root, text='Receita 2')
l_label2.place(x=10, y=40)
e_entry2 = Entry(root)
e_entry2.place(x=100, y=40)
v_result = DoubleVar()
l_result = Label(root, textvariable=v_result)
l_result.place(x=10, y=70)
l_explic = Label(root, text='<-- receita1 x 32%')
l_explic.place(x=100, y=70)
v_result2 = DoubleVar()
l_result2 = Label(root, textvariable=v_result2)
l_result2.place(x=10, y=100)
l_explic2 = Label(root, text='<-- receita2 x 8%')
l_explic2.place(x=100, y=100)
v_result3 = DoubleVar()
l_result3 = Label(root, textvariable=v_result3)
l_result3.place(x=10, y=130)
l_explic3 = Label(root, text='<-- receita1 x 32% + receita2 x 8%')
l_explic3.place(x=100, y=130)
v_result4 = DoubleVar()
l_result4 = Label(root, textvariable=v_result4)
l_result4.place(x=10, y=160)
l_explic4 = Label(root, text='<-- result if')
l_explic4.place(x=100, y=160)
def calc():
receita1 = int(e_entry.get())
receita2 = int(e_entry2.get())
v_result.set(real(receita1 * 32 / 100))
v_result2.set(real(receita2 * 8 / 100))
v_result3.set(real(receita1 * 32 / 100) + (receita2 * 8 / 100))
if v_result3.get() > 60000:
v_result4.set(real((receita1 * 32 / 100) + (receita2 * 8 / 100)) - 60000)
elif v_result3.get() < 60000:
v_result4.set(real(receita1 * 32 / 100) + (receita2 * 8 / 100))
e_entry.delete(0, END)
e_entry2.delete(0, END)
bt = Button(root, text='calc', command=calc)
bt.place(x=10, y=200)
root.mainloop()
答案
您正在看到此问题,因为您正在混合和匹配格式化为字符串和数字本身的数字。
最好将您的计算形成输入/计算/输出。我也很自由地使用Decimal
数字而不是普通整数,因为您似乎正在处理金钱,并且精度通常等同于该域。
from decimal import Decimal
from tkinter import *
root = Tk()
root.geometry("350x350")
l_label = Label(root, text="Receita 1")
l_label.place(x=10, y=10)
e_entry = Entry(root)
e_entry.place(x=100, y=10)
l_label2 = Label(root, text="Receita 2")
l_label2.place(x=10, y=40)
e_entry2 = Entry(root)
e_entry2.place(x=100, y=40)
v_result1 = StringVar()
l_result1 = Label(root, textvariable=v_result1)
l_result1.place(x=10, y=70)
l_explic1 = Label(root, text="<-- receita1 x 32%")
l_explic1.place(x=100, y=70)
v_result2 = StringVar()
l_result2 = Label(root, textvariable=v_result2)
l_result2.place(x=10, y=100)
l_explic2 = Label(root, text="<-- receita2 x 8%")
l_explic2.place(x=100, y=100)
v_result3 = StringVar()
l_result3 = Label(root, textvariable=v_result3)
l_result3.place(x=10, y=130)
l_explic3 = Label(root, text="<-- receita1 x 32% + receita2 x 8%")
l_explic3.place(x=100, y=130)
v_result4 = StringVar()
l_result4 = Label(root, textvariable=v_result4)
l_result4.place(x=10, y=160)
l_explic4 = Label(root, text="<-- result if")
l_explic4.place(x=100, y=160)
def real(my_value):
return str(my_value.quantize(Decimal("0.02"))).replace(".", ",")
def calc():
# Get inputs
receita1 = Decimal(int(e_entry.get()))
receita2 = Decimal(int(e_entry2.get()))
# Compute
result1 = receita1 * Decimal("0.32")
result2 = receita2 * Decimal("0.08")
result3 = receita1 + receita2
if result3 > 60000:
result4 = result3 - 60000
else:
result4 = result3
# Output
v_result1.set(real(result1))
v_result2.set(real(result2))
v_result3.set(real(result3))
v_result4.set(real(result4))
e_entry.delete(0, END)
e_entry2.delete(0, END)
bt = Button(root, text="calc", command=calc)
bt.place(x=10, y=200)
root.mainloop()
以上是关于Python / Tkinter-的主要内容,如果未能解决你的问题,请参考以下文章
在我的代码中添加菜单栏时 Python 中的 TkInter 错误