【问题标题】:How to get value from entry (Tkinter), use it in formula and print the result it in label如何从条目(Tkinter)中获取值,在公式中使用它并将结果打印在标签中
【发布时间】:2019-04-18 04:51:45
【问题描述】:
【问题讨论】:
标签:
python
tkinter
tkinter-entry
【解决方案1】:
您可以通过 .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()
或从文本变量中获取
#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())