簡體   English   中英

如何使用tkinter在新窗口中顯示按鈕命令的輸出?

[英]How can I display output from a button command in a new window using tkinter?

我已經成功創建了一個GUI,該GUI接受用戶輸入並提供所需的輸出,但是我似乎無法弄清楚如何在另一個窗口中而不是僅在IDE控制台中顯示此輸出。 我的目標是,一旦用戶單擊“計算BMI”,就會在輸出中彈出一個窗口,但是到目前為止,輸出僅顯示在控制台中。 我一直在尋找解決方案,但似乎無法弄清楚我可以使用哪些工具來實現這一目標。 我不熟悉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“彈出窗口”通常應通過tk.TopLevel()方法處理! 這將生成一個可以命名的新窗口或在其中放置按鈕,例如:

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()

因此,除了print(bmi)您可以執行以下操作:

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

可以在http://effbot.org/tkinterbook/toplevel.htm上找到更多文檔!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM