我想写一个计算净额和税额的小应用程序。我编写了这段代码,并且我已经多次尝试使用基于此post的var.set()
,但我不知道它是如何正确的。
from tkinter import *
import tkinter as tk
def count23():
b = gross.get()
n = round(b/1.23, 2)
v = round(b - n, 2)
# print here works, but prints in shell
def count8():
b = gross.get()
n = round(b/1.08, 2)
v = round(b - n, 2)
def count5():
b = gross.get()
n = round(b/1.05, 2)
v = round(b - n, 2)
root = tk.Tk()
gross = DoubleVar()
root.geometry('220x200+250+250')
L1 = Label(root, text='Input gross ammount').grid(row=0, column=0, columnspan=5)
E1 = Entry(root, textvariable=gross).grid(row=1, column=1, columnspan=3, sticky='WE', padx=5, pady=5)
L2 = Label(root, text='Choose your tax rate').grid(row=2, column=0, columnspan=5)
B1 = Button(root, text='5 %', command=count5)
B1.grid(row=3, column=0, padx=5, pady=5)
B2 = Button(root, text='8 %', command=count8)
B2.grid(row=3, column=2, padx=5, pady=5)
B3 = Button(root, text='23 %', command=count23)
B3.grid(row=3, column=4, padx=5, pady=5)
L3 = Label(root, text=' ').grid(row=4, column=0, columnspan=5)
L4 = Label(root, text='Net').grid(row=5, column=0, columnspan=2, sticky='WE')
L5 = Label(root, text='TAX').grid(row=5, column=3, columnspan=2, sticky='WE')
L6 = Label(root, relief='raised')
L6.grid(row=6, column=0, columnspan=2, sticky='WE')
L7 = Label(root, relief='raised')
L7.grid(row=6, column=3, columnspan=2, sticky='WE')
root.mainloop()
我需要在L6和L7标签上打印网和税。请给我一些线索,请问。
答案 0 :(得分:4)
这样做的简单方法是为这些标签提供自己的textvariable
s。
我用单个函数count
替换了3 show_tax
个函数。我们在每个Button命令中使用lambda函数以期望的税率调用show_tax
。我还做了一些其他的小改动。
import tkinter as tk
def show_tax(rate):
b = gross.get()
n = round(b / rate, 2)
# Format to string with 2 digits after the decimal point
net.set(format(n, '.2f'))
t = round(b - n, 2)
tax.set(format(t, '.2f'))
root = tk.Tk()
root.geometry('310x165+250+250')
root.title('Tax calculator')
gross = tk.DoubleVar()
net = tk.StringVar()
tax = tk.StringVar()
tk.Label(root, text='Input gross amount').grid(row=0, column=0, columnspan=5)
e = tk.Entry(root, textvariable=gross)
e.grid(row=1, column=1, columnspan=3, sticky='WE', padx=5, pady=5)
tk.Label(root, text='Choose your tax rate').grid(row=2, column=0, columnspan=5)
b = tk.Button(root, text='5 %', command=lambda r=1.05: show_tax(r))
b.grid(row=3, column=0, padx=5, pady=5)
b = tk.Button(root, text='8 %', command=lambda r=1.08: show_tax(r))
b.grid(row=3, column=2, padx=5, pady=5)
b = tk.Button(root, text='23 %', command=lambda r=1.23: show_tax(r))
b.grid(row=3, column=4, padx=5, pady=5)
# An empty Label to force row to be displayed
tk.Label(root).grid(row=4, column=0, columnspan=5)
tk.Label(root, text='Net').grid(row=5, column=0, columnspan=2, sticky='WE')
tk.Label(root, text='TAX').grid(row=5, column=3, columnspan=2, sticky='WE')
l = tk.Label(root, textvariable=net, relief='raised')
l.grid(row=6, column=0, columnspan=2, sticky='WE')
l = tk.Label(root, textvariable=tax, relief='raised')
l.grid(row=6, column=3, columnspan=2, sticky='WE')
root.mainloop()
我对您的代码(以及我对它的更改)提出了更多意见。
使用from tkinter import *
并不是一个好主意,因为它会将130个Tkinter名称导入到全局命名空间中,这很混乱并且可能导致名称冲突。使用显式tk.
表单可以使代码更易于阅读。
顺便说一句,窗口小部件.grid
和.pack
方法返回None
。所以当你做类似的事情时
L2 = Label(root, text='Choose your tax rate').grid(row=2, column=0, columnspan=5)
创建标签,将其放入网格,然后将L2
设置为None
。如果您需要保留对标签的引用,您需要创建小部件和放大器。将它分两步放入网格中,如下所示:
L2 = Label(root, text='Choose your tax rate')
L2.grid(row=2, column=0, columnspan=5)
如果不需要保留对窗口小部件的引用,但您仍希望将其拆分为2行以保持行长度短,那么只需使用“一次性”变量,如我有e
,b
和l
。
正如Bryan Oakley在评论中提到的,我们实际上并不需要为这些标签提供他们自己的StringVar textvariable
:我们可以使用小部件的text
方法直接更新他们的.config
import tkinter as tk
def show_tax(rate):
b = gross.get()
n = round(b / rate, 2)
# Format to string with 2 digits after the decimal point
L6.config(text=format(n, '.2f'))
t = round(b - n, 2)
L7.config(text=format(t, '.2f'))
root = tk.Tk()
root.geometry('310x165+250+250')
root.title('Tax calculator')
gross = tk.DoubleVar()
tk.Label(root, text='Input gross amount').grid(row=0, column=0, columnspan=5)
e = tk.Entry(root, textvariable=gross)
e.grid(row=1, column=1, columnspan=3, sticky='WE', padx=5, pady=5)
tk.Label(root, text='Choose your tax rate').grid(row=2, column=0, columnspan=5)
b = tk.Button(root, text='5 %', command=lambda r=1.05: show_tax(r))
b.grid(row=3, column=0, padx=5, pady=5)
b = tk.Button(root, text='8 %', command=lambda r=1.08: show_tax(r))
b.grid(row=3, column=2, padx=5, pady=5)
b = tk.Button(root, text='23 %', command=lambda r=1.23: show_tax(r))
b.grid(row=3, column=4, padx=5, pady=5)
# An empty Label to force row to be displayed
tk.Label(root).grid(row=4, column=0, columnspan=5)
tk.Label(root, text='Net').grid(row=5, column=0, columnspan=2, sticky='WE')
tk.Label(root, text='TAX').grid(row=5, column=3, columnspan=2, sticky='WE')
L6 = tk.Label(root, relief='raised')
L6.grid(row=6, column=0, columnspan=2, sticky='WE')
L7 = tk.Label(root, relief='raised')
L7.grid(row=6, column=3, columnspan=2, sticky='WE')
root.mainloop()