t说明如何存储输入框中的数字,以便将每个条目都添加到nex

2024-10-04 05:22:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着写一个程序,你在一个输入框里输入一个数字,点击一个按钮,它就会打印出你输入的数字,它会打印出你输入的数字乘以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()

Tags: 代码textcolumn数字rootlabelgridrow
1条回答
网友
1楼 · 发布于 2024-10-04 05:22:03

是什么阻止了你使用正则变量?在

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

相关问题 更多 >