嘗試從使用 tkinker gui 列印到空標簽的函式中獲取結果。我已經搜索并嘗試過自己,但我不知道如何做到這一點。讓它列印到終端沒問題,但列印到標簽上就不行。我一直在看 youtube 教程,并試圖自己破解一些東西。我遇到了很多障礙,但是學習這項技能真是太棒了 :) 如果有人能幫我解決這個問題,我將不勝感激!
代碼如下:
from tkinter import *
root = Tk()
#Labels
head_label = Label(text="Calculates weight pr meter:")
weight_plank_label = Label(text="Weight in gram:")
lenght_plank_label = Label(text="Lenght in mm:")
#Entry field
weight_entry = Entry(root)
lenght_entry = Entry(root)
#Text field label
weight_meter_show = Label(root, text="FUNCTION RESULT HERE", height=1, width=20) # Want result from "def weight():" to show in this Text box
#Calc weight function
def weight():
x = weight_entry.get()
y = lenght_entry.get()
z = (int(x) / int(y))
print(z)
#Button
calc_btn = Button(height=1, width=8, text="Calculate", command=weight)
#Shoving it to root window
head_label.grid(row=0, column=0)
weight_plank_label.grid(row=1, column=0)
lenght_plank_label.grid(row=2, column=0)
weight_entry.grid(row=1, column=1)
lenght_entry.grid(row=2, column=1)
calc_btn.grid(row=3, column=0)
weight_meter_show.grid(row=4, column=0)
root.mainloop()
uj5u.com熱心網友回復:
就像蒂姆羅伯特說的,只需添加:
weight_meter_show.config( text=str(z))
而不是列印在你的體重定義中。
def weight():
x = weight_entry.get()
y = lenght_entry.get()
z = (int(x) / int(y))
weight_meter_show.config(text=str(z))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/383747.html
