从tkvar.get()截断字典

时间:2018-07-06 20:19:39

标签: python string dictionary tkinter

我创建了一个Tkinter窗口,其中包含两个下拉菜单,允许用户选择开始月份和结束月份以计算设置范围内的数据(见下文):

from tkinter import *
from pandas import Timestamp

root = Tk()
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()))
    #range_dict_months = dict(itertools.islice(dict_months.items(), 3))

button = Button(mainframe, text = "OK", command = callback_range)
button.grid(row=3, column=23)

上面的代码产生了这个窗口,除了默认选项是7月18日而不是3月,我在测试代码后对窗口进行了截图:

Above code produces this window

我的两个弹出菜单dict_months的字典是:

>>> dict_months
    {'Jan 17': Timestamp('2017-01-01 00:00:00'), 
    'Feb 17': Timestamp('2017-02-01 00:00:00'), 
    'Mar 17': Timestamp('2017-03-01 00:00:00'), 
    'Apr 17': Timestamp('2017-04-01 00:00:00'), 
    'May 17': Timestamp('2017-05-01 00:00:00'), 
    'Jun 17': Timestamp('2017-06-01 00:00:00'), 
    'Jul 17': Timestamp('2017-07-01 00:00:00'), 
    'Aug 17': Timestamp('2017-08-01 00:00:00'), 
    'Sep 17': Timestamp('2017-09-01 00:00:00'), 
    'Oct 17': Timestamp('2017-10-01 00:00:00'), 
    'Nov 17': Timestamp('2017-11-01 00:00:00'), 
    'Dec 17': Timestamp('2017-12-01 00:00:00'), 
    'Jan 18': Timestamp('2018-01-01 00:00:00'), 
    'Feb 18': Timestamp('2018-02-01 00:00:00'), 
    'Mar 18': Timestamp('2018-03-01 00:00:00'), 
    'Apr 18': Timestamp('2018-04-01 00:00:00'), 
    'May 18': Timestamp('2018-05-01 00:00:00'), 
    'Jun 18': Timestamp('2018-06-01 00:00:00'), 
    'Jul 18': Timestamp('2018-07-01 00:00:00')}

我想更改我的callback_range()函数,使其包括从用户输入中截断/切片字典。例如,如果用户选择如图所示的1月17日至3月17日,则所需的输出应为:

>>> dict
{'Jan 17': Timestamp('2017-01-01 00:00:00'), 
'Feb 17': Timestamp('2017-02-01 00:00:00'), 
'Mar 17': Timestamp('2017-03-01 00:00:00')}

如何使用tkvar_start.get()tkvar_end.get()截断字典,并在字典之间加上月份?字典是不可迭代的,我可能是错的,但是使用iterslice似乎需要使用索引。

islice(sequence, start, stop, step)

1 个答案:

答案 0 :(得分:1)

这可能不是最好的方法,但是您可以执行以下操作:

def callback_range():
    list_of_keys  = list(dict_months.keys())    
    # approach 1
    range_dict = { k:dict_months[k] for k in list_of_keys[list_of_keys.index(tkvar_start.get()):list_of_keys.index(tkvar_end.get())+1]} 
    print(range_dict)
    # approach 2
    import itertools
    range_dict = dict(itertools.islice(dict_months.items(),list_of_keys.index(tkvar_start.get()), list_of_keys.index(tkvar_end.get())+1)) 
    print(range_dict)

输出:

{'Jan 17': Timestamp('2017-01-01 00:00:00'), 'Feb 17': Timestamp('2017-02-01 00:00:00'), 'Mar 17': Timestamp('2017-03-01 00:00:00')}
{'Jan 17': Timestamp('2017-01-01 00:00:00'), 'Feb 17': Timestamp('2017-02-01 00:00:00'), 'Mar 17': Timestamp('2017-03-01 00:00:00')}