我正在尝试编写一个程序,您可以在输入框中输入一个数字,单击一个按钮,它会打印您输入的数字,并打印您输入的数字乘以 0.008。
然后存储数字,以便下次输入数字时,它会将其添加到前一个数字中并打印出来,依此类推。我已经编写了第一段代码,效果很好。但无论我做多少研究,我都找不到如何做第二点。这是我到目前为止的代码。
from tkinter import *
def calculatemoney():
done = float(Lines1.get())
salary3 = done * 0.08
salary4 = done * 1
labelresult = Label(root, text='%.0f' % salary4).grid(row=3, column=2)
labelresult = Label(root, text=' £ %.2f' % salary3).grid(row=4, column=2)
root = Tk()
root.title('Dict8 Calc')
root.geometry('250x200+800+100')
Lines1 = StringVar()
var1 = Label(root, text='Enter Lines').grid(row=0, column=1)
var2 = Label(root, text='Lines Today').grid(row=3, column=1)
var3 = Label(root, text='Money Today').grid(row=4, column=1)
var4 = Label(root, text='Lines Total').grid(row=6, column=1)
var5 = Label(root, text='Money Total').grid(row=7, column=1)
myLines = Entry(root, textvariable=Lines1).grid(row=0, column=2)
button1 = Button(root, text=' Calculate ', command=calculatemoney).grid(row=8, column=2)
root.mainloop()
是什么阻止您使用常规变量?
from tkinter import *
def calculatemoney():
global oldValue # Making it global so you can set it's value
done = float(Lines1.get())
salary3 = done * 0.08
salary4 = done
salary5 = (done + oldValue) * 0.8 # Adding the old value to the new one
salary6 = done + oldValue
Label(root, text='%.0f' % salary4).grid(row=3, column=2) # I don't recommend this method of putting a label over another every time the user activates this function
Label(root, text=' f %.2f' % salary3).grid(row=4, column=2)
Label(root, text='%.0f' % salary6).grid(row=6, column=2)
Label(root, text=' f %.2f' % salary5).grid(row=7, column=2)
oldValue += done # Adding the current value to the old value
root = Tk()
oldValue = 0.0 # Define variable that will represent an old value
root.title('Dict8 Calc')
root.geometry('250x200+800+100')
Lines1 = StringVar()
var1 = Label(root, text='Enter Lines').grid(row=0, column=1) # .grid() method returns 'None' so you dont have any use for 'var1'.
var2 = Label(root, text='Lines Today').grid(row=3, column=1)
var3 = Label(root, text='Money Today').grid(row=4, column=1)
var4 = Label(root, text='Lines Total').grid(row=6, column=1) # Shouldn't it be 'row=5' ?
var5 = Label(root, text='Money Total').grid(row=7, column=1)
myLines = Entry(root, textvariable=Lines1).grid(row=0, column=2)
button1 = Button(root, text=' Calculate ', command=calculatemoney).grid(row=8, column=2)
root.mainloop()