如何从条目(Tkinter)中获取值,在公式中使用它并将结果打印在标签中 [英] How to get value from entry (Tkinter), use it in formula and print the result it in label

查看:41
本文介绍了如何从条目(Tkinter)中获取值,在公式中使用它并将结果打印在标签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Tkinter的函数入口时,可以写一个字符串值,用它做事;但我实际上是在使用公式.这个想法相当简单:在一堆盒子里填上数字(压力、推力、应力、温度等),然后取这些数字,应用公式并在同一个窗口中显示结果.

When using the function entry of Tkinter, you can write a string value and do things with it; but I'm actually working with formulas. The idea is fairly simple: to put a bunch of boxes to fill with numbers (pressure, thrust, stress, temperature and so on) and then take that numbers, apply the formulas and show the results in the same window.

我该怎么做?

我一直在寻找一个小时又一个小时,但没有得到一个不令人困惑的解决方案.

I've been searching for hours and hours without getting a non confusing solution.

好像这个页面上的那个人也遇到了同样的问题,但我对解决方案一无所知:如何从条目(Tkinter)中获取值,在公式中使用它并将结果打印在标签中

Seems like the guy in this page had the same trouble, but I did not understand a nut of the solution: How to get value from entry(Tkinter), use it in formula and print the result it in label

这里也是我能得到的少数(只有 2 个)的另一个例子,但对我来说,比上面的要复杂得多:

Here as well is another example of the few (just 2) I could get, but for me, is way complicated than the previous above:

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

如果有人可以分享一个完整的程序来解释我如何在未来的项目中应用从字符串条目中获取数值的概念,我会非常高兴.

If someone could share a full program which explains me how can I apply that concept of taking numerical values from string entries in my future projects, I will be so so so glad.

推荐答案

您可以通过 .get() 从小部件中获取值

You can either get values by .get() from widgets

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 获取

or get it from 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())

这篇关于如何从条目(Tkinter)中获取值,在公式中使用它并将结果打印在标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆