【问题标题】:Insert set values in same order they appear in code如何以与设置顺序相同的方式将值插入到 ListBox
【发布时间】:2022-11-05 21:53:49
【问题描述】:

我想要的只是以与集合中相同的顺序插入列表框项目。我希望它像这样插入:'damp'->'camp'->'came' 等等。我怎样才能做到这一点?

    gui_dict = tk.Listbox(third_task_window, width=10, height=10)
    words = {'damp', 'camp', 'came', 'lame', 'lime', 'like',
             'cold', 'card', 'cord', 'corm', 'worm', 'warm'}
    for word in words:
        gui_dict.insert(tk.END, word)

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    你把它们放在一个集合{} 中,它根据创建时的哈希值进行排序,将它们放在一个列表[] 中,它们不会被排序,它们的顺序与你放入它们的顺序相同。

    import tkinter as tk
    
    root = tk.Tk()
    gui_dict = tk.Listbox(root, width=10, height=10)
    words = ['damp', 'camp', 'came', 'lame', 'lime', 'like',
             'cold', 'card', 'cord', 'corm', 'worm', 'warm']
    for word in words:
        gui_dict.insert(tk.END, word)
    gui_dict.pack()
    
    root.mainloop()
    

    请参阅 python 数据结构文档:https://docs.python.org/3/tutorial/datastructures.html

    【讨论】:

      猜你喜欢
      • 2014-07-28
      • 2011-06-16
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多