I have just started learning how to code, if anyone could help me it would be so appreciated!
All I am trying to do is to print my results on the GUI, I'm aware that printing goes straight to the terminal but I'm not sure how to figure this one out...
from tkinter import *
window = Tk() #instantiate an instance of a window
window.geometry("360x360")
window.title("Gold River 2.0")
window.config(background="#181818")
#enter weekly earnings
earnings = Entry()
earnings.pack()
#calculate button
def calculate():
total = earnings.get()
print("$",total,"earnt")
free = int(total) * 0.25
print("FREE =",free)
savings = int(total) * 0.30
print("SAVINGS =",savings)
business = int(total) * 0.15
print("BUSINESS =",business)
investment = int(total) * 0.20
print("INVESTMENTS =",investment)
gifts = int(total) * 0.10
print("GIFTS =",gifts)
calculate = Button(window, text="Calculate", command=calculate)
calculate.pack()
window.mainloop() #places window on computer screen
CodePudding user response:
You can't use Python's built in print()
function to display the output on the Tkinter window, it is meant to be used to print a given message to the screen or to display an object’s value on the terminal.
So, instead of using print() you can Tkinter Label
widgets, which are used to display a text or image on the screen.
Your Code can be modified like below:
from tkinter import *
window = Tk() #instantiate an instance of a window
window.geometry("360x360")
window.title("Gold River 2.0")
window.config(background="#181818")
#enter weekly earnings
earnings = Entry()
earnings.pack()
# Calculate button's function
def calculate():
total = float(earnings.get())
# label which shows value of 'total'
total_label = Label(window, text=f"${total} earnt")
total_label.pack()
free = int(total) * 0.25
# label which shows value of 'free'
free_label = Label(window, text=f"FREE = {free}")
free_label.pack()
savings = int(total) * 0.30
# label which shows value of 'savings'
savings_label = Label(window, text=f"SAVINGS = {savings} earnt")
savings_label.pack()
business = int(total) * 0.15
# label which shows value of 'business'
business_label = Label(window, text=f"BUSINESS = {business}")
business_label.pack()
investment = int(total) * 0.20
# label which shows value of 'investment'
investment_label = Label(window, text=f"INVESTMENTS = {investment}")
investment_label.pack()
gifts = int(total) * 0.10
# label which shows value of 'gifts'
gifts_label = Label(window, text=f"GIFTS = {gifts}")
gifts_label.pack()
calculate = Button(window, text="Calculate", command=calculate)
calculate.pack()
window.mainloop() #places window on computer screen
To know more about Tkinter Label
widgets -> here