我只会发布我的代码片段,因为本质上,它做同样的事情。
string = ''
time_calc = Tk()
time_calc.geometry('500x400')
time_calc.title("Calculate A Time")
time_calc_frame= Frame(time_calc).grid(row=0, column=0)
jul_box = Entry(time_calc)
jul_box.insert(0, "Julian Date")
jul_box.pack(side = TOP)
jul_box.bind('<Return>')
def jd2gd(jd):
global string
jd=jd+0.5
Z=int(jd)
F=jd-Z
alpha=int((Z-1867216.25)/36524.25)
A=Z + 1 + alpha - int(alpha/4)
B = A + 1524
C = int( (B-122.1)/365.25)
D = int( 365.25*C )
E = int( (B-D)/30.6001 )
dd = B - D - int(30.6001*E) + F
if E<13.5:
mm=E-1
if E>13.5:
mm=E-13
if mm>2.5:
yyyy=C-4716
if mm<2.5:
yyyy=C-4715
months=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
daylist=[31,28,31,30,31,30,31,31,30,31,30,31]
daylist2=[31,29,31,30,31,30,31,31,30,31,30,31]
h=int((dd-int(dd))*24)
min =int((((dd-int(dd))*24)-h)*60)
sec=86400*(dd-int(dd))-h*3600-min*60
# Now calculate the fractional year. Do we have a leap year?
if (yyyy%4 != 0):
days=daylist2
elif (yyyy%400 == 0):
days=daylist2
elif (yyyy%100 == 0):
days=daylist
else:
days=daylist2
hh = 24.0*(dd % 1.0)
min = 60.0*(hh % 1.0)
sec = 60.0*(min % 1.0)
dd = dd-(dd%1.0)
hh = hh-(hh%1.0)
min = min-(min%1.0)
if hh < 10:
hh = '0%s'%str(int(hh))
else:
hh = int(hh)
if min < 10:
min = '0%s'%str(int(min))
else:
min = int(min)
if sec < 10:
sec = '0%s'%str(int(sec))
else:
sec = int(sec)
string = "%s/%s/%s %s:%s:%s UTC" % (str(int(mm)), str(int(dd)), str(yyyy), str(hh), str(min), str(sec))
print string
return
def j2ghandler():
jul = float(jul_box.get())
jd2gd(jul)
j2g_button = Button(time_calc, text='Convert Julian\n to Gregorian', command = j2ghandler, width= 25, pady=5).pack(side=TOP)
#Draw Time String on Canvas
var = StringVar()
var.set(string)
label = Label(time_calc, textvariable=var)
label.pack(side=TOP)
我知道,“片段”。当您输入数字并单击按钮时应该发生什么,它应该并且确实在幕后更改字符串“string”。我无法让标签更新。如您所见,我尝试使用 StringVar() 来更新标签。另外,我尝试.update()
在函数结束时进行操作,但没有成功。
对此事的任何帮助将不胜感激。这是应用程序的唯一部分,无论我尝试什么,我都无法让它工作。
谢谢您的帮助。
PS我没有包括所有这些from Tkinter import *
,它是多余的,对于这段代码来说不是必需的。