Default to and select first item in Tkinter listbox

sedeh

I want to automatically select the first item in the listbox. By selecting the first item, I don't mean simply defaulting to the first item or setting focus on it. I've already achieved that by doing self.listbox.select_set(0). I want the default item also selected. In other words, when I run my code below, I want print(value) to print the value of the default selection. If Asia is chosen from the optionmenu, Japan should automatically print to the console. If Africa, Nigeria should print and Germany for Europe.

Any suggestion on how I can achieve this? Thanks.

from tkinter import *
from tkinter import ttk
import tkinter.messagebox

class App:
    def __init__(self):
        self.master = Tk()
        self.di = {'Asia': ['Japan', 'China', 'Malaysia', 'India', 'Korea',
                            'Vietnam', 'Laos', 'Thailand', 'Singapore',
                            'Indonesia', 'Taiwan'],
                     'Europe': ['Germany', 'France', 'Switzerland'],
                     'Africa': ['Nigeria', 'Kenya', 'Ethiopia', 'Ghana',
                                'Congo', 'Senegal', 'Guinea', 'Mali', 'Cameroun',
                                'Benin', 'Tanzania', 'South Africa', 'Zimbabwe']}
        self.variable_a = StringVar()
        self.frame_optionmenu = ttk.Frame(self.master)
        self.frame_optionmenu.pack()
        options = sorted(self.di.keys())
        self.optionmenu = ttk.OptionMenu(self.frame_optionmenu, self.variable_a, options[0], *options)

        self.variable_a.set('Asia')
        self.optionmenu.pack()
        self.btn = ttk.Button(self.master, text="Submit", width=8, command=self.submit)
        self.btn.pack()

        self.frame_listbox = ttk.Frame(self.master)

        self.frame_listbox.pack(side=RIGHT, fill=Y)
        self.scrollbar = Scrollbar(self.frame_listbox )
        self.scrollbar.pack(side=RIGHT, fill=Y)
        self.listbox = Listbox(self.frame_listbox, selectmode=SINGLE, yscrollcommand=self.scrollbar.set)
        self.variable_a.trace('w', self.updateoptions)

        self.scrollbar.config(command=self.listbox.yview)
        self.listbox.pack()

        #Populate listbox
        for each in self.di[self.variable_a.get()]:
            self.listbox.insert(END, each)
            self.listbox.select_set(0) #This only sets focus on the first item.
        self.listbox.bind("<<ListboxSelect>>", self.OnSelect)

        self.master.mainloop()

    def updateoptions(self, *args):
        #countries = self.di[self.variable_a.get()]
        self.listbox.delete(0, 'end')
        for each in self.di[self.variable_a.get()]:
            self.listbox.insert(END, each)
            self.listbox.select_set(0) #This only sets focus on the first item.
        self.listbox.pack()

    def submit(self, *args):
        var = self.variable_a.get()
        if messagebox.askokcancel("Selection", "Confirm selection: " + var):
            print(var)

    def OnSelect(self, event):
        widget = event.widget
        value = widget.get(widget.curselection()[0])
        print(value)

App()

Running Python 3.4.1

Bryan Oakley

The simplest solution is to generate the <<ListboxSelect>> event at the same time that you change the selection:

def updateoptions(self, *args):
    ...
    self.listbox.select_set(0) #This only sets focus on the first item.
    self.listbox.event_generate("<<ListboxSelect>>")
    ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

TKinter ListBox项目高度

来自分类Dev

Tkinter-循环更新ListBox

来自分类Dev

Python Tkinter Listbox 文件实现

来自分类Dev

tkinter.Listbox滚动条yview

来自分类Dev

使用 Tkinter Listbox 作为打印的替代方法

来自分类Dev

shift +click functionality on listbox item using WPF

来自分类Dev

Changing background of listbox item on mouse over

来自分类Dev

WPF Listbox item searching using keyboard

来自分类Dev

Why my currently add listbox item is not accessible?

来自分类Dev

Tkinter Listbox小部件是否有“包含”方法

来自分类Dev

Tkinter Listbox小部件是否有“包含”方法

来自分类Dev

是否可以在执行期间更改 tkinter ListBox 的选择模式?

来自分类Dev

ListView only shows first item

来自分类Dev

Linq select item after if statement

来自分类Dev

MySQL INSERT INTO ... SELECT or default value

来自分类Dev

Select all choices in a ManyToManyField by default

来自分类Dev

Can I bind to the first item of a CollectionView?

来自分类Dev

Accordion - How to not have first item expanded?

来自分类Dev

Disable the default constructor on a Database-First workflow

来自分类Dev

在Tkinter中切换窗口时,Python-Listbox项不会终止

来自分类Dev

读取txt文件并将内容添加到Tkinter Listbox Python

来自分类Dev

我可以在循环时在Tkinter Listbox中分配和访问变量吗?

来自分类Dev

如何使用Tkinter Listbox小部件在Sqlite3表中获得一行?

来自分类Dev

python tkinter listbox与其他tk / ttk小部件的性能

来自分类Dev

Selenium IDE select2 selecting an item

来自分类Dev

Is simple_list_item_single_choice the default layout for ListPreference items?

来自分类Dev

Google Maps API'.pac-item:first'返回的格式错误

来自分类Dev

How can I select the item that doesn't have attribute

来自分类Dev

iOS UITableViewCell Have To Hold A Long Press To Select Item

Related 相关文章

  1. 1

    TKinter ListBox项目高度

  2. 2

    Tkinter-循环更新ListBox

  3. 3

    Python Tkinter Listbox 文件实现

  4. 4

    tkinter.Listbox滚动条yview

  5. 5

    使用 Tkinter Listbox 作为打印的替代方法

  6. 6

    shift +click functionality on listbox item using WPF

  7. 7

    Changing background of listbox item on mouse over

  8. 8

    WPF Listbox item searching using keyboard

  9. 9

    Why my currently add listbox item is not accessible?

  10. 10

    Tkinter Listbox小部件是否有“包含”方法

  11. 11

    Tkinter Listbox小部件是否有“包含”方法

  12. 12

    是否可以在执行期间更改 tkinter ListBox 的选择模式?

  13. 13

    ListView only shows first item

  14. 14

    Linq select item after if statement

  15. 15

    MySQL INSERT INTO ... SELECT or default value

  16. 16

    Select all choices in a ManyToManyField by default

  17. 17

    Can I bind to the first item of a CollectionView?

  18. 18

    Accordion - How to not have first item expanded?

  19. 19

    Disable the default constructor on a Database-First workflow

  20. 20

    在Tkinter中切换窗口时,Python-Listbox项不会终止

  21. 21

    读取txt文件并将内容添加到Tkinter Listbox Python

  22. 22

    我可以在循环时在Tkinter Listbox中分配和访问变量吗?

  23. 23

    如何使用Tkinter Listbox小部件在Sqlite3表中获得一行?

  24. 24

    python tkinter listbox与其他tk / ttk小部件的性能

  25. 25

    Selenium IDE select2 selecting an item

  26. 26

    Is simple_list_item_single_choice the default layout for ListPreference items?

  27. 27

    Google Maps API'.pac-item:first'返回的格式错误

  28. 28

    How can I select the item that doesn't have attribute

  29. 29

    iOS UITableViewCell Have To Hold A Long Press To Select Item

热门标签

归档