0

Hello I am new to python and trying to get my head around Tkinter. I am trying to make an application where you can input loans and investments. There's 2 text boxes in this program and if you make an investment or take loan, the program prints those in text box. The other text box should show your loans and investments in total, but I have a couple of issues:

1) The textbox always shows the newest input, not the total of my inputs.

2) The textbox shows either my investment or my loan, but I want it to show both of them, my total loan and my total investments.

Can anyone help?

import tkinter as tk

root = tk.Tk()

canvas = tk.Canvas(height=700, width=900)
canvas.pack()

frame = tk.Frame(root, bd=5)
frame.place(relx=0.05, rely=0.05, relwidth=0.9, relheight=0.9)

button1 = tk.Button(frame, text="Loan", font="fill")
button2 = tk.Button(frame, text="Investment", font="fill")
entry1 = tk.Entry(frame, font="fill")
entry2 = tk.Entry(frame, font="fill")
textbox1 = tk.Text(frame, font="fill")
textbox2 = tk.Text(frame, font="fill")

button1.place(relx=0.1, rely=0.1, relwidth=0.3, relheight=0.15)
button2.place(relx=0.1, rely=0.3, relwidth=0.3, relheight=0.15)
entry1.place(relx=0.5, rely=0.1, relwidth=0.4, relheight=0.15)
entry2.place(relx=0.5, rely=0.3, relwidth=0.4, relheight=0.15)
textbox1.place(relx=0.1, rely=0.5, relwidth=0.3975, relheight=0.4)
textbox2.place(relx=0.5, rely=0.5, relwidth=0.4, relheight=0.4)

def printer1(event):
    loan = 0
    entered_text = float(tk.Entry.get(entry1))
    loan += entered_text
    if entered_text:
        textbox1.insert(tk.END, "New loan taken: " + str(loan) + " €" + "\n")
        textbox2.delete(1.0, tk.END)
        textbox2.insert(1.0, "Your loan in total: " + str(loan) + " €" + "\n")
    else:
        pass

def printer2(event):
    investment = 0
    entered_text = float(tk.Entry.get(entry2))
    investment += entered_text
    if entered_text:
        textbox1.insert(tk.END, "Money invested: " + str(investment) + " €" + "\n")
        textbox2.delete(1.0, tk.END)
        textbox2.insert(1.0, "Your investments in total: " + str(investment) + " €" + "\n")
    else:
        pass

button1.bind("<Button-1>", printer1)
button2.bind("<Button-1>", printer2)

root.mainloop()
Programmer
  • 306
  • 2
  • 8

2 Answers2

1

Your variables loan and investment are local variables within the functions which get reset to 0 every time the functions are called. The easiest way is to move them outside of the function, and then declare global inside the functions:

import tkinter as tk

root = tk.Tk()

...

loan = 0
investment = 0

def printer1(event):
    entered_text = float(tk.Entry.get(entry1))
    global loan
    ...

def printer2(event):
    global investment
    entered_text = float(tk.Entry.get(entry2))
    ...
Henry Yik
  • 22,275
  • 4
  • 18
  • 40
0

I would like to answer.

import tkinter as tk

root = tk.Tk()

canvas = tk.Canvas(height=700, width=900)
canvas.pack()

frame = tk.Frame(root, bd=5)
frame.place(relx=0.05, rely=0.05, relwidth=0.9, relheight=0.9)

button1 = tk.Button(frame, text="Loan", font="fill")
button2 = tk.Button(frame, text="Investment", font="fill")
entry1 = tk.Entry(frame, font="fill")
entry2 = tk.Entry(frame, font="fill")

textbox1 = tk.Text(frame, font="fill")
textbox2 = tk.Text(frame, font="fill")

button1.place(relx=0.1, rely=0.1, relwidth=0.3, relheight=0.15)
button2.place(relx=0.1, rely=0.3, relwidth=0.3, relheight=0.15)
entry1.place(relx=0.5, rely=0.1, relwidth=0.4, relheight=0.15)
entry2.place(relx=0.5, rely=0.3, relwidth=0.4, relheight=0.15)
textbox1.place(relx=0.1, rely=0.5, relwidth=0.3975, relheight=0.4)
textbox2.place(relx=0.5, rely=0.5, relwidth=0.4, relheight=0.4)


history_loans=[]
history_investments=[]


def printer1(event):
    loan = 0
    entered_text = float(tk.Entry.get(entry1))
    loan += entered_text

    history_loans.append(loan)

    if entered_text:
        textbox1.insert(tk.END, "New loan taken: " + str(loan) + " €" + "\n")
        textbox2.delete(1.0, tk.END)

        textbox2.insert(1.0, "Your loan in total: " + str(sum(history_loans)) + " €" + "\n")
        textbox2.insert(1.0, "Your investments in total: " + str(sum(history_investments)) + " €" + "\n")

    else:
        pass

def printer2(event):
    investment = 0
    entered_text = float(tk.Entry.get(entry2))
    investment += entered_text

    history_investments.append(investment)

    if entered_text:
        textbox1.insert(tk.END, "Money invested: " + str(investment) + " €" + "\n")
        textbox2.delete(1.0, tk.END)


        textbox2.insert(1.0, "Your loan in total: " + str(sum(history_loans)) + " €" + "\n")
        textbox2.insert(1.0, "Your investments in total: " + str(sum(history_investments)) + " €" + "\n")    
    else:
        pass

button1.bind("<Button-1>", printer1)
button2.bind("<Button-1>", printer2)

root.mainloop()

New variables:

history_loans=[]
history_investments=[]

How can we have a historical: (To see what .append does see this link)

history_loans.append(loan)
history_investments.append(investment)

How to print in both cases:

textbox2.insert(1.0, "Your loan in total: " + str(sum(history_loans)) + " €" + "\n")
textbox2.insert(1.0, "Your investments in total: " + str(sum(history_investments)) + " €" + "\n")    
Pablo Díaz
  • 330
  • 4
  • 14