from Tkinter import *
# --- ---
# global variable to use in different functions
win = None
def get_input(text, variable):
global win # inform function to use global variable `win` to assign value
win = Toplevel()
win.title("Get value")
f = LabelFrame(win, text=text)
f.pack()
# use `f` instead of `win` to put inside LabelFrame
e = Entry(f, textvariable=variable)
e.pack()#side="right")
e.bind("<Return>", lambda event:do_more(variable))
b = Button(win, text="OK", command=lambda:do_more(variable))
b.pack()
def do_more(variable):
data = variable.get()
print 'do_more:', data, len(data), type(data)
win.destroy()
# --- ---
def get_value(text, variable):
# create window
get_input(text, variable)
# this code is executed directly after creating window, not after closing window
# so code has to wait till window is closed
win.wait_window()
data = variable.get()
print 'get_value:', data, len(data), type(data)
# --- main --
root = Tk()
root.title("Ask-name-SUB")
# global variables
var_name = StringVar()
var_address = StringVar()
b = Button(root, text="Enter your name", command=lambda:get_value("Your name:", var_name))
b.pack()
b = Button(root, text="Enter your address", command=lambda:get_value("Your address:", var_address))
b.pack()
b = Button(root, text="Cancel", command=root.destroy)
b.pack()
root.mainloop()
是INFO
我正在使用list
进行排序,并希望输出采用字典的形式。但是,我得到了不必要的字符:
Counter
不必要的字符是:
wanted_dict = collections.Counter(INFO)
>>> Counter({'BLA': 102, 'BLABLA': 96, 'BLABLABLA': 96...})
有没有办法输出以下内容?
Counter
(
)
答案 0 :(得分:2)
将其包裹在dict
电话中。
>>> dict(Counter({'BLA': 102, 'BLABLA': 96, 'BLABLABLA': 96}))
{'BLABLABLA': 96, 'BLABLA': 96, 'BLA': 102}
您可以使用此方法将大多数特殊collections
字典对象转换为普通字典。