官术网_书友最值得收藏!

How to do it...

The following program creates a list selection with the days of the week. There is a button to print the actual selection and a list of buttons to change the selection mode:

import tkinter as tk 
 
DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", 
        "Friday", "Saturday", "Sunday"] 
MODES = [tk.SINGLE, tk.BROWSE, tk.MULTIPLE, tk.EXTENDED] 
 
class ListApp(tk.Tk): 
    def __init__(self): 
        super().__init__() 
        self.list = tk.Listbox(self)  
        self.list.insert(0, *DAYS) 
        self.print_btn = tk.Button(self, text="Print selection", 
                                   command=self.print_selection) 
        self.btns = [self.create_btn(m) for m in MODES] 
 
        self.list.pack() 
        self.print_btn.pack(fill=tk.BOTH) 
        for btn in self.btns: 
            btn.pack(side=tk.LEFT) 
 
    def create_btn(self, mode): 
        cmd = lambda: self.list.config(selectmode=mode) 
        return tk.Button(self, command=cmd, 
                         text=mode.capitalize()) 
 
    def print_selection(self): 
        selection = self.list.curselection() 
        print([self.list.get(i) for i in selection]) 
 
if __name__ == "__main__": 
    app = ListApp() 
    app.mainloop() 

You can try out changing the mode of selection and printing the selected items:

主站蜘蛛池模板: 梁平县| 独山县| 和政县| 白朗县| 澜沧| 亚东县| 德保县| 泌阳县| 桐城市| 永和县| 措美县| 景东| 都昌县| 视频| 丹江口市| 湖口县| 张家界市| 长乐市| 米脂县| 乳源| 新建县| 丰都县| 弥渡县| 汪清县| 建始县| 榆中县| 手游| 平泉县| 巴彦县| 大埔县| 名山县| 武定县| 张掖市| 米脂县| 特克斯县| 江安县| 泽库县| 拉孜县| 郸城县| 太康县| 开化县|