我正在尝试使用 Python Tkinter 在相应项目中为我的所有传感器的“最后一次看到时间”构建一个有组织的列表。
底部框架(无论是在两个列表框下的什么),假设每次单击“发送到检查”按钮时都会刷新。
当窗口处于全屏模式时,它确实没问题。但是,当窗口没有足够的空间显示底部框架时,在第一次单击时,它只显示部分文本标签并调整窗口大小(请参见附图)。放置在底部框架中的所有小部件仅在第二次单击时成功显示。
此问题在每次单击时交替出现。
我想知道为什么会这样。在此先感谢您的帮助 !!
下面是按下按钮时调用的代码。
def check_instruments(exist_instruments):
global tablelabel
global timelabel
global bottom_tableFrame
global bottomtop_Frame
global bottombottom_Frame
global now
tablelabel.destroy()
timelabel.destroy()
bottom_tableFrame.destroy()
bottomtop_Frame.destroy()
bottombottom_Frame.destroy()
all_items = instrument_menu.get(0, END)
sel_idx = instrument_menu.curselection()
sel_list = [all_items[item] for item in sel_idx]
if len(sel_list) <=20:
sel_instrument_info_list=[]
for sel in sel_list:
for instrument in exist_instruments:
if sel == instrument.name:
sel_instrument_info_list.append(instrument)
bottom_tableFrame = Frame(win)
bottomtop_Frame = Frame(bottom_tableFrame)
bottombottom_Frame = Frame(bottom_tableFrame)
tablelabel = Label(bottomtop_Frame, text="Instrument Last Seen Information", pady=5)
tablelabel.pack()
timelabel = Label(bottomtop_Frame, text="Time Compared: " + str(now), font='Helvetica 9', padx=30, fg="grey")
timelabel.pack(side=RIGHT)
bottombottom_Frame.columnconfigure((0,2), weight=1)
for i in range(len(sel_instrument_info_list)):
for j in range(3):
if j == 0:
current_entry = Label(bottombottom_Frame, bg = "white", text=sel_instrument_info_list[i].name, font='Helvetica 12 bold')
current_entry.grid(row=i, column=j,sticky="EW")
if sel_instrument_info_list[i].overtime == True:
current_entry.config(fg= "red" )
elif j==1:
current_entry = Label(bottombottom_Frame, bg = "white", text=sel_instrument_info_list[i].id)
current_entry.grid(row=i, column=j,sticky="EW")
elif j==2:
current_entry = Label(bottombottom_Frame, bg = "white", text=sel_instrument_info_list[i].lastseentime)
current_entry.grid(row=i, column=j,sticky="EW")
if win.attributes("-zoomed") ==0:
win.geometry("")
bottom_tableFrame.pack(fill=BOTH, expand= 1)
bottomtop_Frame.pack(fill=X)
bottombottom_Frame.pack(fill=BOTH, padx=30, pady=20)
并且标签和框架在脚本的最开始声明如下:
bottomtop_Frame = Frame(bottom_tableFrame)
bottombottom_Frame = Frame(bottom_tableFrame)
tablelabel=Label(bottomtop_Frame)
timelabel=Label(bottomtop_Frame)