因此,我正在开发一种表观温度计算工具,以集成到具有许多不同功能的数据分析程序中。我的“测试代码”在我的主程序之外运行良好,并给出了正确的结果。有效的代码:
from numpy import arange, exp, vectorize, size, zeros, seterr, where, log10, all
from scipy.constants import h,c,k,pi
from scipy.interpolate import interp1d
from scipy.integrate import quad
def planck_l(l, T):
c1 = 2*h*c**2
c2 = h*c / k
M = (c1/l**5) * (1 / (exp(c2/(l*T))-1))
return M
def planck_p(l, T):
c1 = 2*pi*c
c2 = h*c / k
M = (c1/(pi*l**4)*(1/(exp(c2/(l*T))-1)))
return M
def integrand(planck, lband, rband, T):
return quad(planck, lband, rband, args=T)[0]
def getRadiance(planck,lband,rband,T):
int_planck = vectorize(integrand)
return int_planck(planck,lband,rband,T)
def rad2t(rad, l, r):
temps = arange(0, 5000)
radiance = []
for t in temps:
radiance.append(integrand(planck_l, l/1e6, r/1e6, t)/100/100)
f = interp1d(radiance, temps)
T = f(rad)
return T
但是,当我将此代码集成到我的主 tkinter GUI 循环中时,我定义的被积函数仅返回 0.0 的值。不起作用的代码:
import tkinter as tk
from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
from tkinter import filedialog, messagebox
from glob import glob
import re
import os
import sys
import tkinter.scrolledtext as tkst
import struct
import cv2
import csv
from scipy.constants import h, c, k, pi
from scipy.integrate import quad
from scipy.interpolate import interp1d
import bigfloat
def planck_l(l, T):
c1 = 2*h*c**2
c2 = h*c / k
M = (c1/l**5) * (1 / (np.exp(c2/(l*T))-1))
return M
def planck_p(l, T):
c1 = 2*pi*c
c2 = h*c / k
M = (c1/(pi*l**4)*(1/(np.exp(c2/(l*T))-1)))
return M
def integrand(planck, lband, rband, T):
return quad(planck, lband, rband, args=T)[0]
def getRadiance(planck,lband,rband,T):
int_planck = np.vectorize(integrand)
return int_planck(planck, lband, rband, T)
def rad2T(rad, l, r):
temps = np.arange(0, 5000)
radiance = []
for t in temps:
radiance.append(integrand(planck_l, l/1e6, r/1e6, t)/100/100)
f = interp1d(radiance, temps)
T = f(rad)
return T
#.......... other definitions, main window/widget building etc. ...........
def rad_t_conv(self, coor):
window = tk.Toplevel(self)
window.geometry(coor[0] + coor[1])
window.grab_set()
Label(window, text='Set Wavelength Range (um): ').grid(row=0, column=0, columnspan=2)
range = StringVar()
range.set('3-5')
range_enter = Entry(window, textvariable=range, width=10)
range_enter.grid(row=1, column=0)
window.rngsub = tk.Button(window, text='Save', command=lambda: self.submit(range_enter, range))
window.rngsub.grid(row=2, column=0)
Label(window, text='Radiance:').grid(row=3, column=0)
rad = StringVar()
rad_enter = Entry(window, textvariable=rad, width=10)
rad_enter.grid(row=4, column=0)
window.radsub = tk.Button(window, text='Calculate App. Temp', command=lambda: self.combine(self.submit(rad_enter, rad), self.calc_temp(rad.get(), range.get(), t_enter, temp)))
window.radsub.grid(row=5, column=0)
Label(window, text='App. Temperature:').grid(row=3, column=1)
temp = StringVar()
t_enter = Entry(window, textvariable=temp, width=10)
t_enter.grid(row=4, column=1)
window.tempsub = tk.Button(window, text='Calculate Radiance', command=lambda: self.combine(self.submit(t_enter, temp), self.calc_rad(temp.get(), range.get(), rad_enter, rad)))
window.tempsub.grid(row=5, column=1)
def calc_temp(self, rad, range, temp_ent, temp):
range = range.split('-')
rad = float(rad)
l = float(range[0])
r = float(range[1])
T = rad2t(rad, l, r)
temp.set(str(T))
self.update_idletasks()
def calc_rad(self, temp, range, rad_ent, rad):
range = range.split('-')
l = int(range[0])
r = int(range[1])
temp = float(temp)
radiance = integrand(planck_l, l/1e6, r/1e6, temp)/100/100
print(radiance)
rad.set(str(radiance))
self.update_idletasks()
我认为这可能是一个四舍五入的问题,但使用 bigfloat 没有效果。我真的很困惑为什么它会在我的主代码之外工作,然后在集成时以这种方式失败。我一直在搜索我的 tkinter gui 窗口代码的其余部分,以寻找任何可能影响它的东西,但我很困惑。任何帮助,甚至猜测,都非常感谢。如果需要,我可以发布更多我的主要代码,但老实说,这是一团糟,因为我一直在使用 pyinstaller 将其编译为 .exe,并且它需要在同一个文件中定义所有非模块函数。为了回答@jsbueno,我对 rad_t_conv 的调用来自我主窗口中的一个按钮。
self.rad2t_start = tk.Button(self, text='Radiance/Apparent Temp Conversion', command=lambda: self.rad_t_conv(coor))
self.rad2t_start.grid(row=0, column=2, pady=5)
类定义如下:
class Application(tk.Frame):
然后称为:
root = tk.Tk()
w = 700 # width for the Tk root
h = 500 # height for the Tk root
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
app = Application(master=root)
app.pack(side="top", fill="both", expand=True)
root.mainloop()
合并函数为:
def combine(self, *funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combined_func