我正在尝试构建一个简单的计算器。t1
我想在文本框中插入一个计算。例如,如果t2
文本框和t3
文本框被填充或更改t3
文本框会自动进行计算。如何让我的t1
文本框有一个 while 循环来连续计算其他两个框中的值?
这是我的代码,如果有什么令人困惑的地方,请告诉我!谢谢!
import tkinter as tk
import tkinter.font as TkFont
root = tk.Tk()
#helv36 = TkFont.Font(family="courier new",size=10,weight="bold")
#e1 = tk.Text(root, bg='black', fg='white', font=helv36)
#e1.pack()
#row=0, column=0, rowspan=0, columnspan=0
def open_pl():
pl = tk.Toplevel()
pl.title('P/L')
pl.geometry('305x255')
#label
l1 = tk.Label(pl, text='Profit/Loss Calculator', padx=90, pady=4).grid(row=0, column=0,columnspan=2)
l2 = tk.Label(pl, text='Initial Balance: ',justify='left', anchor='w').grid(row=2, column=0, sticky='w')
l3 = tk.Label(pl, text='Balance: ',justify='left', anchor='w').grid(row=3, column=0, sticky='w')
l4 = tk.Label(pl, text='thinkorswim: ********', padx=85, pady=4).grid(row=5, column=0,columnspan=2)
#text box
t1 = tk.Text(pl, height=2, width=20, padx=70, pady=15)
t2 = tk.Text(pl, height=2, width=20, padx=20).grid(row=2, column=1,columnspan=4)
t3 = tk.Text(pl, height=2, width=20, padx=20).grid(row=3, column=1,columnspan=4)
t4 = tk.Text(pl, height=2, width=20, padx=70, pady=15).grid(row=4, column=0,columnspan=2)
t1.grid(row=1, column=0,columnspan=2)
# i tried it here
while t3 != 0:
initial_balance = t2.get('1.0', INSERT)
balance = t3.get('1.0', INSERT)
t1.insert('1.0', (float(balance)-float(initial_balance))/float(initial_balance))
menubar = tk.Menu(root)
m1 = tk.Menu(menubar, tearoff=0)
m1.add_command(label="P/L", command=open_pl)
menubar.add_cascade(label="Tools", menu=m1)
root.config(menu=menubar)
root.mainloop()