0

I am trying to build a simple calculator. I want insert a calculation inside the t1 text box. For example if t2 text box and t3 text box is filled or change t3 text box would automatically do the calculation. How can I make my t1 text box have a while loop that continuously calculates the value in the other two boxes?

Here's my code, if anything is confusing please let me know! Thank you!

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

0 Answers0