首先,我不是专家,我对tkinter知之甚少。 我正在使用openpyxl来处理excel文件。我已经有一个杂乱的脚本,可以正常运行(在MS-DOS中)。问题是当我想做某事时,我一次又一次地重新执行脚本并设置提示。 因此,我创建了一个tkinter gui应用程序,其中有一个主框架和第二个框架,我称之为“处理框架”。在过程框架中,我具有OptionMenu,该菜单具有来自excel文件的工作表名称,当我选择工作表名称并单击“源工作表”按钮时,有一个名为“源工作表”的按钮,它将创建另一个选项菜单,该菜单列出工作表第一行的标题。因此,正如您所看到的,它依赖于第一个OptionMenu,我认为我必须销毁先前列出标题的OptionMenu,因为它们相互重叠。
在创建一个菜单之前,我曾尝试销毁该产品,我认为它可以解决问题,但是第一个OptionMenu尚未定义,因此我不知道如何销毁。
class ProcessFrame(tk.Frame):
def __init__(self, parent, controller, source, target):
tk.Frame.__init__(self, parent)
self.controller = controller
self.source_wb = source
self.target_wb = target
self.source_WS = None
self.target_WS = None
self.source_ETS = None
self.target_ETS = None
self.source_Data = None
self.target_Data = None
#source sheet and header setup
self.s_sheet_choice = tk.StringVar()
self.s_sheet_choices = self.source_wb.sheetnames
self.s_sheet_popupMenu = tk.OptionMenu(self, self.s_sheet_choice, *self.s_sheet_choices)
self.lbl = tk.Label(self, text="Choose Source Sheet:").grid(row = 1, column = 1)
self.s_sheet_popupMenu.grid(row = 2, column = 1)
#self.s_sheet_choice.trace('w', self.change_dropdown_source_sheet)
#if self.source_WS:
#source sheet has been set, it is not None anymore
self.s_sheet_check_btn = tk.Button(self, text = "Source Sheet", command=self.check_source_sheet).grid(row = 1, column = 2)
#ef change_dropdown_source_sheet(self, *args):
#self.s_sheet_choice.get()
def check_source_sheet(self):
self.source_WS = self.source_wb[self.s_sheet_choice.get()]
self.s_header_list = self.source_WS[1]
self.s_new_header_list = {}
for count, cell in enumerate(self.s_header_list, 1):
if cell == None:
continue
self.s_new_header_list[cell.value] = "{}:{}".format(cell.value,str(count))
print(self.s_new_header_list[cell.value])
self.s_header_c = tk.StringVar()
self.s_header_lbl = tk.Label(self, text="Choose the header:").grid(row = 4 , column = 1)
self.s_header_popupMenu = tk.OptionMenu(self, self.s_header_c, *self.s_new_header_list).grid(row = 5, column = 1)
self.set_header_button = tk.Button(self, text="set ets column", command=self.set_ets_column).grid(row = 4, column = 2)
def set_ets_column(self):
self.source_ETS = int(self.s_new_header_list[self.s_header_c.get().split(":")[0]].split(":")[1])
print(self.source_WS.cell(row=100, column=self.source_ETS).value