簡體   English   中英

如何從條目(Tkinter)中獲取值,在公式中使用它並將結果打印在標簽中

[英]How to get value from entry (Tkinter), use it in formula and print the result it in label

使用Tkinter的函數入口時,可以寫一個字符串值,用它做事; 但我實際上是在使用公式。 這個想法相當簡單:把一堆盒子填上數字(壓力、推力、應力、溫度等),然后取這些數字,應用公式並在同一個窗口中顯示結果。

我怎樣才能做到這一點?

我一直在尋找幾個小時和幾個小時,但沒有得到一個不令人困惑的解決方案。

似乎這個頁面中的那個人有同樣的問題,但我不明白解決方案的本質如何從條目(Tkinter)中獲取價值,在公式中使用它並將結果打印在標簽中

這里也是我能得到的少數(只有 2 個)的另一個例子,但對我來說,比上面的要復雜得多:

https://www.python-course.eu/tkinter_entry_widgets.php

如果有人可以分享一個完整的程序來解釋我如何在未來的項目中應用從字符串條目中獲取數值的概念,我會非常高興。

您可以通過 .get() 從小部件獲取值

from tkinter import *
#Create the window
myWindow = Tk()

#Define your formula here
def MyCalculateFunction():

    #Get your value from box_pressure
    #Remember to convert string to integer or float / double
    pressure, temprature = float(box_pressure.get()), float(box_temprature.get())
    result = pressure + temprature

    #Show your result with label
    label_result.config(text="%f + %f = %f" % (pressure, temprature, result))

#Create a input box for pressure
box_pressure = Entry(myWindow)
box_pressure.pack()

#Create a input box for temprature
box_temprature = Entry(myWindow)
box_temprature.pack()

#Create a button
button_calculate = Button(myWindow, text="Calcuate", command=MyCalculateFunction)
button_calculate.pack()

#Create a label
label_result = Label(myWindow)
label_result.pack()

或從 textvariable 獲取

#Bind it with variable
variable_pressure = DoubleVar()
box_pressure = Entry(myWindow, textvariable=variable_pressure)
box_pressure.pack()

#Get/Set value by .get() / .set()
variable_pressure.set(42)

# shows 42
print(variable_pressure.get())

暫無
暫無

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

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