简体   繁体   English

按字母顺序排序Tkinter选项菜单?

[英]Sort a Tkinter optionmenu alphabetically?

I'm trying to make an OptionMenu in Tkinter that lists it's data Alphabetically, but I have no idea how. 我正在尝试在Tkinter中创建一个按字母顺序列出数据的OptionMenu,但我不知道如何。

This is my code for the OptionMenu data set. 这是我的OptionMenu数据集的代码。 (This will expand as I develop the program). (这将在我开发程序时扩展)。

data={
'Actually Additions Atomic Reconstructor',
'Advanced Mortars',
'Artisan Worktables',
'Extra Utilities Crusher',
'Extra Utilities Resonator',
'Initial Inventory',
'JEI Hide',
'JEI RemoveAndHide',
'Ore Dictionary Add',
'Ore Dictionary Create',
'Ore Dictionary Remove',
'Seed Drops'
}

And this is my code for the OptionMenu. 这是我的OptionMenu代码。

var = tkinter.StringVar()
var.set('Advanced Mortars')
p = tkinter.OptionMenu(window, var, *data)
p.config(font='Helvetica 12 bold')
p.pack()

Every time I run the code and open the OptionMenu, everything is scrambled randomly. 每次我运行代码并打开OptionMenu时,一切都会随机乱码。 How do I sort this alphabetically? 我该如何按字母顺序排序?

Since data is a set, which is an unordered collection in python, the data will always be scrambled. 由于data是一个集合,它是python中的无序集合,因此数据将始终被加扰。 Since data already looks sorted, an easy way to fix this is to change data to a list, which is an ordered collection: 由于data已经看起来已经排序,因此解决此问题的一种简单方法是将data更改为列表,这是一个有序集合:

data=[
     'Actually Additions Atomic Reconstructor',
     'Advanced Mortars',
     ...
     ]

If your data has to be a set to begin with, you could also sort it beforehand with sorted() : 如果您的数据必须是一个开头的集合,您也可以事先使用sorted()对其进行sorted()

data = sorted(data)

Your code runs fine when I run this: 运行此代码时,您的代码运行正常:

from tkinter import *

data={
      'Actually Additions Atomic Reconstructor',
      'Advanced Mortars',
      'Artisan Worktables',
      'Extra Utilities Crusher',
      'Extra Utilities Resonator',
      'Initial Inventory',
      'JEI Hide',
      'JEI RemoveAndHide',
      'Ore Dictionary Add',
      'Ore Dictionary Create',
      'Ore Dictionary Remove',
      'Seed Drops'
     }

data = sorted(data)

master = Tk()

var = StringVar(master)
var.set('Advanced Mortars')
p = OptionMenu(master, var, *data)
p.config(font='Helvetica 12 bold')
p.pack()

Thankfully, this solution is fairly simple and intuitive. 值得庆幸的是,这个解决方案相当简单直观。 All you have to do is add in a sorted() when you pass the argument: 您所要做的就是在传递参数时添加sorted():

var = tkinter.StringVar()
var.set('Advanced Mortars')
p = tkinter.OptionMenu(window, var, *sorted(data))
p.config(font='Helvetica 12 bold')
p.pack()

Just make sure you put the sorted() inside the * splat operator as sorted() needs to be applied to a list such as your data variable but with sorted(*data) it actually treats *data as a bunch of individual variables so sorted() won't work. 只需确保将sorted()放在* splat运算符中,因为sorted()需要应用于列表(如数据变量),但是对于sorted(*data)它实际上将*data视为一堆单独的变量,这样sorted()将无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM