0

I have successfully created a GUI that takes user input and gives the desired output, but I can't seem to figure out how to display this output in another window instead of just in the IDE console. My goal is to have a window pop up with the output once the user clicks 'Compute BMI', but as of right now, the output only shows in the console. I have looked for solutions but I can't seem to figure out what tools I can use to make this happen. I am new to GUIs so any help would be much appreciated.

from tkinter import *

root = Tk()

def myBMI():
    weight = float(Entry.get(weight_field))
    height = float(Entry.get(height_field))
    bmi = (weight*703)/(height*height)
    print(bmi)

height_label = Label(root, text="Enter your height: ")
height_field = Entry(root)
height_field.grid(row=0, column=1)
height_label.grid(row=0, sticky=E)

weight_label = Label(root, text="Enter your weight: ")
weight_field = Entry(root)
weight_field.grid(row=1, column=1)
weight_label.grid(row=1, sticky=E)

compute_bmi = Button(root, text="Compute BMI", command=myBMI)
compute_bmi.grid(row=2)

root.mainloop()
2
  • Have you learned about the Toplevel widget? Commented Mar 18, 2019 at 20:45
  • This is the first I am hearing about it, I'm looking into it now Commented Mar 18, 2019 at 20:54

1 Answer 1

2

tkinter "pop-ups" should typically be handled via the tk.TopLevel() method! This will generate a new window that can be titled or have buttons put in it like:

top = Toplevel()
top.title("About this application...")

msg = Message(top, text=about_message)
msg.pack()

button = Button(top, text="Dismiss", command=top.destroy)
button.pack()

So instead of print(bmi) you could do something like, say:

top = tk.Toplevel()
msg = tk.Label(top, text=bmi)
msg.pack()

More documentation can be found at http://effbot.org/tkinterbook/toplevel.htm!

Sign up to request clarification or add additional context in comments.

2 Comments

Ahh, okay this makes a lot of sense. Thank you!
@Brent Glad to help! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.