简体   繁体   English

如何使用tkinter在新窗口中显示按钮命令的输出?

[英]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.

相关问题 如何在新窗口中显示图形? 图形用户界面 - How can I display a graph in a new window? Tkinter GUI Tkinter - 我如何制作一个按钮可以创建新的 window 并关闭主 window - Tkinter - How can I make a button can create new window and close the main window 获取命令窗口输出以使用tkinter在窗口小部件中显示 - Get command window output to display in widget with tkinter 如何创建“控制台”显示以显示我在 tkinter 窗口中运行的代码的输出? - How can I create a "Console" Display to show the output of code I am running in a tkinter window? 如何使用 ```import Tkinter``` 而不是 ```from tkinter import *``` 设置 Tkinter window 的大小? - How can I set a size for a Tkinter window using ```import Tkinter``` instead of ```from tkinter import *```? 在Tkinter按钮命令上显示新图像 - Display new image on Tkinter button command 如何从第二个 window 在 tkinter 中将 function 作为按钮命令运行 - How to run a function as a button command in tkinter from a 2nd window 如何在新的Tkinter窗口上显示字典 - How do i display dictionaries on new tkinter window 如何在新的 Tkinter window 中显示导入的 dataframe? - How do i display imported dataframe in new Tkinter window? 如何通过单击带有 tkinter 的按钮打开新的 window? - How do i open a new window on the click of a button with tkinter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM