tkinter如何从输入框中存储数字,以便将每个条目添加到下一个条目

时间:2016-06-07 11:43:05

标签: python python-3.x tkinter tkinter-entry

我正在尝试编写一个程序,您可以在其中输入一个数字到输入框中单击按钮,它会打印您输入的数字,并打印您输入的数字乘以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()

1 个答案:

答案 0 :(得分:0)

什么阻止你使用常规变量?

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()