销毁现有标签Tkin

2024-06-07 21:11:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用python中的Tkinter运行多个UI计算,其中我必须显示所有计算的所有输出。问题是,第一次计算的输出是正常的,但进一步计算的输出似乎是用默认值计算的。我知道我应该销毁第一个标签以输出第二个计算结果,但是当我试图销毁我的第一个标签时,我不能。我尝试的代码如下:

from tkinter import *

def funcname():
#My calculations
   GMT = GMT_user.get()
   lat = lat_deg_user.get()

   E = GMT * 365
   Eqntime_label.configure(text=E)


   Elevation = E/lat
   Elevation_label.configure(text=Elevation)

nUI_pgm = Tk()
GMT_user = DoubleVar()
lat_deg_user = DoubleVar()

nlabel_time = Label(text = "Enter time in accordance to GMT in decimal").pack()
nEntry_time = Entry(nUI_pgm, textvariable = GMT_user).pack()

nlabel_Long = Label(text = "Enter Longitude in Decimal Degrees").pack()
nEntry_Long = Entry(nUI_pgm, textvariable = lat_deg_user).pack()

nbutton = Button(nUI_pgm, text = "Calculate", command = funcname).pack()


#Displaying results

nlabel_E = Label (text = "The Equation of Time is").pack()
Eqntime_label = Label(nUI_pgm, text="")
Eqntime_label.pack()
#when i try
Eqntime_label.destroy() # this doesn't work

nlabel_Elevation = Label(text = "The Elevation of the sun is").pack()
Elevation_label = Label(nUI_pgm, text="")
Elevation_label.pack()

nUI_pgm.mainloop() 

在这里,我必须在结果显示后销毁Eqntime_标签,以便输出Elevation_标签。我该怎么办??在


Tags: texttime标签labelpacklatelevationgmt
2条回答

你有几个错误:

def funcname()
GMT = GMT_user.get()

第一行缺少冒号,第二行缺少缩进->

^{pr2}$

此处变量名错误(并且pack返回None):

Entry_Long = Entry(nUI_pgm, textvariable = long_deg_user).pack()

必须是:

Entry(nGui_pgm, textvariable = lat_deg_user).pack()

“我知道我应该销毁第一个标签以输出第二个计算”:不,configure方法是一个好方法。所以删除:

Eqntime_label.destroy()

使用set而不是{}的版本,这更符合代码中的其他变量定义。
此外,必须缩进与函数对应的所有行。
其余的,已经在其他答案中解释过了。在

from tkinter import *

def funcname():
    #My calculations
    GMT = GMT_user.get()
    lat = lat_deg_user.get()

    E = GMT * 365
    Elevation = E/lat

    etime.set(E)
    elevation.set(Elevation)


root = Tk()

# Getting user input
GMT_user = DoubleVar()
lat_deg_user = DoubleVar()

Label(root, text="Enter time in accordance to GMT in decimal").pack()
nEntry_time = Entry(root, textvariable=GMT_user).pack()

Label(root, text="Enter Latitude in Decimal Degrees").pack()
nEntry_Long = Entry(root, textvariable=lat_deg_user).pack()

nbutton = Button(root, text="Calculate", command=funcname).pack()

# Displaying results
etime = StringVar()
elevation = StringVar()

Label(text="The Equation of Time is").pack()
Eqntime_label = Label(root, textvariable=etime)
Eqntime_label.pack()

Label(text="The Elevation of the sun is").pack()
Elevation_label = Label(root, textvariable=elevation)
Elevation_label.pack()

root.mainloop()

相关问题 更多 >

    热门问题