【发布时间】:2018-12-31 05:37:03
【问题描述】:
(我为这个幼稚的问题道歉)
我有一个弹出窗口,允许用户选择两个月作为数据收集的起点和终点。
单击“确定”按钮时发生的事件是返回所选范围之间月份的列表和字典,并通知用户所选范围。但是,我也希望在单击“确定”后关闭此窗口。这些事件在callback 函数下。我无法使root.destroy 可访问。
所有涉及的代码都在这里。我的函数callback 是朝向底部的:
from tkinter import *
startDate = 'Dec 16'
now = datetime.now()
endDate = now.strftime('%b %y')
start = datetime.strptime(startDate, '%b %y').date()
end = datetime.strptime(endDate, '%b %y').date()
cur_date = start
range_months = []
while cur_date < end:
cur_date += relativedelta(months=1)
range_months.append(cur_date)
range_months = [month.strftime('%b %y') for month in range_months]
dict_months = dict((month, pd.to_datetime(month, format='%b %y')) for month in range_months)
root = Tk()
#root.withdraw()
root.title("Select your Month Range")
# Setting up Grid
mainframe = Frame(root)
mainframe.pack(pady=50, padx=50)
Label(mainframe, text="Select Start Month").grid(row=1, column=1)
Label(mainframe, text="Select End Month").grid(row=1, column=6)
# Creating two Tkinter variables to obtain user input from the drop down menus
tkvar_start = StringVar(root)
tkvar_end = StringVar(root)
tkvar_start.set('Jan 17') # Setting a default option
tkvar_end.set(endDate)
start_popupMenu = OptionMenu(mainframe, tkvar_start, *dict_months)
end_popupMenu = OptionMenu(mainframe, tkvar_end, *dict_months)
start_popupMenu.grid(row=2, column=1)
end_popupMenu.grid(row=2, column=6)
def callback_range():
print('The range is from %s to %s' % (tkvar_start.get(), tkvar_end.get()))
list_of_month_keys = list(dict_months.keys())
import itertools
range_dict_months = dict(itertools.islice(dict_months.items(), list_of_month_keys.index(tkvar_start.get()),
list_of_month_keys.index(tkvar_end.get()) + 1))
return list_of_month_keys, range_dict_months
button = Button(mainframe, text = "OK", command = callback_range)
button.grid(row=3, column=23)
root.mainloop()
list_of_month_keys, range_dict_months = callback_range()
如何在我的函数callback 中使root.destroy() 可访问,同时仍返回list_of_month_keys 和range_dict_months,以便在单击“确定”时另外关闭此窗口?
【问题讨论】:
-
你不能。该函数将在完成 return 语句后停止执行。处理完数据后,您需要重新编写程序以关闭 GUI 窗口。
-
您的最后一行代码
list_of_month_keys, range_dict_months = callback_range()将永远无法工作。原因是您的函数callback_range()在您的 tkinter 主循环中,并且您的最后一行代码将永远无法执行此函数。即使主循环终止了该循环内的所有内容。