- Tkinter GUI Application Development Cookbook
- Alejandro Rodas de Paz
- 128字
- 2021-08-27 19:44:04
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:

推薦閱讀
- Android應用程序開發與典型案例
- 自己動手實現Lua:虛擬機、編譯器和標準庫
- Network Automation Cookbook
- Python Deep Learning
- Access 2010數據庫基礎與應用項目式教程(第3版)
- Android玩家必備
- Android移動開發案例教程:基于Android Studio開發環境
- Unity&VR游戲美術設計實戰
- Mastering Python Design Patterns
- Java圖像處理:基于OpenCV與JVM
- Python Digital Forensics Cookbook
- Visual C++從入門到精通(第2版)
- 精通Spring:Java Web開發與Spring Boot高級功能
- UML基礎與Rose建模實用教程(第三版)
- 大話程序員:從入門到優秀全攻略