[英]How can I display output from a button command in a new window using tkinter?
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. 我已经成功创建了一个GUI,该GUI接受用户输入并提供所需的输出,但是我似乎无法弄清楚如何在另一个窗口中而不是仅在IDE控制台中显示此输出。 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.
我的目标是,一旦用户单击“计算BMI”,就会在输出中弹出一个窗口,但是到目前为止,输出仅显示在控制台中。 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.
我不熟悉GUI,因此不胜感激。
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()
tkinter "pop-ups" should typically be handled via the tk.TopLevel()
method! tkinter“弹出窗口”通常应通过
tk.TopLevel()
方法处理! 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: 因此,除了
print(bmi)
您可以执行以下操作:
top = tk.Toplevel()
msg = tk.Label(top, text=bmi)
msg.pack()
More documentation can be found at http://effbot.org/tkinterbook/toplevel.htm ! 可以在http://effbot.org/tkinterbook/toplevel.htm上找到更多文档!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.