OptionMenu won't show the first option when clicked (Tkinter)

非 Y 不嫁゛ 提交于 2019-12-23 09:53:42

问题


I added a OptionMenu widget to my code, and assigned a list as it's options. This is how it is:

z = StringVar()   
z.set(userList[0])    
usersOption = OptionMenu(frame1, z, *userList)#, command=changeUser)
usersOption.pack(side=RIGHT, padx=3)

Now, I reckon it would show all the options in said list. As so:

Option 1 \/ <-- the box with the selected option
Option 1 }\__the options that show on click
Option 2 }/

but it actually only shows the second option, and when I choose it there is, basically, no way back, if I click the box again it keeps only showing option 2 and I can't change it even with the up and down keys. I tried looking for solutions, but I got nowhere, so I'm starting to think it is the default operating way of the widget, but I found nothing to show me how to solve it in the documentation I read.
P.S.: I'm using Python 3.3


回答1:


I had the same problem and it was driving me mad, so i looked in the source. I think the issue is that the 3rd constructor argument is the default value. If you don't specify it before *userList, it looks like it takes the first item as the default value. A real fix would be something like:

z = StringVar()   
z.set(userList[0])    
usersOption = OptionMenu(frame1, z, userList[0] ,*userList)#, command=changeUser)
usersOption.pack(side=RIGHT, padx=3)



回答2:


Late answer..

Just use

self.option = OptionMenu(PARENT, VALUE TO BE CHANGED, "DEFAULT TEXT", *OPTIONS_ARRAY/LIST)

Works perfectly for me.




回答3:


Never mind, I took the *userList off and used a for loop to insert the items as commands. Now it works just fine.
The code I used:

for user in userList:
    usersOption["menu"].insert("end", "command", label=user, command=_setit(z, user, changeUser))


来源:https://stackoverflow.com/questions/16514138/optionmenu-wont-show-the-first-option-when-clicked-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!