Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将用户值存储在输入框中,以计算Tkinter中的值_Python_Tkinter - Fatal编程技术网

Python 将用户值存储在输入框中,以计算Tkinter中的值

Python 将用户值存储在输入框中,以计算Tkinter中的值,python,tkinter,Python,Tkinter,我试图使用python中的Tkinter从用户输入计算一个等式。代码如下: import math from tkinter import * def Solar_Param(): d = Interface.get() S_E = 1367*(1 + 0.0334 * math.cos(((math.pi*360)/180) * (d - 2.7206) / 365.25)) nlabel1 = Label(nGui, text = S_E).pack(side="lef

我试图使用python中的Tkinter从用户输入计算一个等式。代码如下:

import math

from tkinter import *

def Solar_Param():
   d = Interface.get()
   S_E = 1367*(1 + 0.0334 * math.cos(((math.pi*360)/180) * (d - 2.7206) / 365.25))
   nlabel1 = Label(nGui, text = S_E).pack(side="left")
   return S_E

nGui = Tk()
Interface = IntVar()

nGui.title("Solar Calculations")

nlabel = Label(text = "User Interface for Solar Calculation")
nlabel.pack()

nbutton = Button(nGui, text = "Calculate", command = Solar_Param).pack()
nEntry = Entry(nGui, textvariable = Interface).pack()

nGui.mainloop()
这里,S_E的值是使用默认值d即0自动计算的,我不希望这样。即使我在UI中将输入更改为其他值,输出仍然是默认值


我尝试使用self方法,但我的上级不希望代码变得复杂。我应该怎么做才能在不改变源代码的情况下计算S_E的值呢?

您的计算似乎很好。我认为问题在于,你不断地创建新标签而不破坏旧标签,因此你看不到新的计算结果

创建一次结果标签,然后为每次计算修改它:

import math

from Tkinter import *

def Solar_Param():
   d = Interface.get()
   S_E = 1367*(1 + 0.0334 * math.cos(((math.pi*360)/180) * (d - 2.7206) / 365.25))

   result_label.configure(text=S_E)
   return S_E

nGui = Tk()
Interface = IntVar()

nGui.title("Solar Calculations")

nlabel = Label(text = "User Interface for Solar Calculation")
nlabel.pack()

nbutton = Button(nGui, text = "Calculate", command = Solar_Param).pack()
nEntry = Entry(nGui, textvariable = Interface).pack()

result_label = Label(nGui, text="")
result_label.pack(side="top", fill="x")

nGui.mainloop()

首先请阅读:@Ericleveil:虽然这是个好建议,但与问题中的问题无关。