- Tkinter GUI Application Development Cookbook
- Alejandro Rodas de Paz
- 151字
- 2021-08-27 19:44:07
How to do it…
We will define a Frame subclass to represent a scrollable list, and then create two instances of this class. The two buttons will also be directly added to the main window:
import tkinter as tk
class ListFrame(tk.Frame):
def __init__(self, master, items=[]):
super().__init__(master)
self.list = tk.Listbox(self)
self.scroll = tk.Scrollbar(self, orient=tk.VERTICAL,
command=self.list.yview)
self.list.config(yscrollcommand=self.scroll.set)
self.list.insert(0, *items)
self.list.pack(side=tk.LEFT)
self.scroll.pack(side=tk.LEFT, fill=tk.Y)
def pop_selection(self):
index = self.list.curselection()
if index:
value = self.list.get(index)
self.list.delete(index)
return value
def insert_item(self, item):
self.list.insert(tk.END, item)
class App(tk.Tk):
def __init__(self):
super().__init__()
months = ["January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"]
self.frame_a = ListFrame(self, months)
self.frame_b = ListFrame(self)
self.btn_right = tk.Button(self, text=">",
command=self.move_right)
self.btn_left = tk.Button(self, text="<",
command=self.move_left)
self.frame_a.pack(side=tk.LEFT, padx=10, pady=10)
self.frame_b.pack(side=tk.RIGHT, padx=10, pady=10)
self.btn_right.pack(expand=True, ipadx=5)
self.btn_left.pack(expand=True, ipadx=5)
def move_right(self):
self.move(self.frame_a, self.frame_b)
def move_left(self):
self.move(self.frame_b, self.frame_a)
def move(self, frame_from, frame_to):
value = frame_from.pop_selection()
if value:
frame_to.insert_item(value)
if __name__ == "__main__":
app = App()
app.mainloop()
推薦閱讀
- JBoss Weld CDI for Java Platform
- 多媒體CAI課件設計與制作導論(第二版)
- Learn to Create WordPress Themes by Building 5 Projects
- SQL學習指南(第3版)
- 簡單高效LATEX
- Xamarin.Forms Projects
- Python編程與幾何圖形
- 快速念咒:MySQL入門指南與進階實戰
- PHP+MySQL+Dreamweaver動態網站開發從入門到精通(第3版)
- 硬件產品設計與開發:從原型到交付
- Spark技術內幕:深入解析Spark內核架構設計與實現原理
- Unity 5 Game Optimization
- 劍指大數據:企業級電商數據倉庫項目實戰(精華版)
- Learning Node.js for Mobile Application Development
- Jenkins 2.x實踐指南