提供编辑列表框的标准 ttk 方式
Standard ttk way to provide a list box with editing
我需要一个具有多项选择的列表控件以及一种供用户编辑显示列表的方法。
根据 Python - python-list - ttk Listbox 判断,ttk.Treeview
是显示列表的新 black 方式和 Tkinter.Listbox
的替代方式。
是否也提供了一些 stock/recommended 合并列表编辑的方法?
这通常是通过控件周围某处的三个按钮 "add"/"edit"/"delete" 完成的(前两个可能会导致带有编辑控件的小 window弹出),或者列表条目本身成为一个编辑器,例如双击。手动实现任一逻辑都非常重要。
Tk,因此 Tkinter 只提供基础部件;它们的可重用组合超出了它们的范围。被遗弃的 Tix 包提供了一种机制和它们的集合,称为 "mega-widgets",但这个想法显然没有流行起来。
我按照我的方式按照以下方式(使用模型-视图-展示器设计模式)做到了。
看起来像这样:
import tkinter
from tkinter import ttk
from tkinter import W, E
class View_EditableList(object):
def __init__(self,root,root_row):
""" List with buttons to edit its contents.
:param root: parent widget
:param roow_row: row in `root'. The section takes 2 rows.
"""
self.list = tkinter.Listbox(root, selectmode=tkinter.EXTENDED)
self.list.grid(row=root_row, sticky=(W, E))
root.rowconfigure(root_row, weight=1)
self.frame = ttk.Frame(root)
self.frame.grid(row=root_row+1, sticky=(W, E))
self.add = ttk.Button(self.frame, text="+", width=5)
self.add.grid(row=0, column=1)
self.edit = ttk.Button(self.frame, text="*", width=5)
self.edit.grid(row=0, column=2)
self.del_ = ttk.Button(self.frame, text="-", width=5)
self.del_.grid(row=0, column=3)
self.up = ttk.Button(self.frame, text=u"↑", width=5)
self.up.grid(row=0, column=4)
self.down = ttk.Button(self.frame, text=u"↓", width=5)
self.down.grid(row=0, column=5)
self.frame.grid_columnconfigure(0, weight=1)
self.frame.grid_columnconfigure(6, weight=1)
class Presenter_EditableList(object):
def __init__(self,view,root):
"""
:param view: View_EditableList
:param root: root widget to be used as parent for modal windows
"""
self.root = root
self.view = view
view.add.configure(command=self.add)
view.edit.configure(command=self.edit)
view.del_.configure(command=self.del_)
view.up.configure(command=self.up)
view.down.configure(command=self.down)
def add(self):
w=View_AskText(self.root)
self.root.wait_window(w.top)
if w.value:
self.view.list.insert(self.view.list.size(),w.value)
def edit(self):
l=self.view.list
try:
[index]=l.curselection()
except ValueError:
return
w=View_AskText(self.root,l.get(index))
self.root.wait_window(w.top)
if w.value:
l.delete(index)
l.insert(index,w.value)
def del_(self):
l=self.view.list
try:
[index]=l.curselection()
except ValueError:
return
l.delete(index)
l.select_set(max(index,l.size()-1))
def up(self):
l = self.view.list
try:
[index] = l.curselection()
except ValueError:
return
if index>0:
v = l.get(index)
l.delete(index)
l.insert(index-1,v)
l.select_set(index-1)
def down(self):
l = self.view.list
try:
[index] = l.curselection()
except ValueError:
return
if index<l.size()-1:
v = l.get(index)
l.delete(index)
l.insert(index+1,v)
l.select_set(index+1)
def getlist(self):
return [self.view.list.get(i) for i in range(self.view.list.size())]
def setlist(self,list_):
self.view.list.delete(0,tkinter.END)
for i,v in enumerate(list_):
self.view.list.insert(i,v)
# supplemental class; it's in another file in my actual code
class View_AskText(object):
"""
A simple dialog that asks for a text value.
"""
def __init__(self, master, value=u""):
self.value = None
top = self.top = tkinter.Toplevel(master)
top.grab_set()
self.l = ttk.Label(top, text=u"Value:")
self.l.pack()
self.e = ttk.Entry(top)
self.e.pack()
self.b = ttk.Button(top, text='Ok', command=self.save)
self.b.pack()
if value: self.e.insert(0, value)
self.e.focus_set()
top.bind('<Return>', self.save)
def save(self, *_):
self.value = self.e.get()
self.top.destroy()
root = tkinter.Tk()
view = View_EditableList(root, 5)
Presenter_EditableList(view, root)
root.mainloop()
我需要一个具有多项选择的列表控件以及一种供用户编辑显示列表的方法。
根据 Python - python-list - ttk Listbox 判断,ttk.Treeview
是显示列表的新 black 方式和 Tkinter.Listbox
的替代方式。
是否也提供了一些 stock/recommended 合并列表编辑的方法?
这通常是通过控件周围某处的三个按钮 "add"/"edit"/"delete" 完成的(前两个可能会导致带有编辑控件的小 window弹出),或者列表条目本身成为一个编辑器,例如双击。手动实现任一逻辑都非常重要。
Tk,因此 Tkinter 只提供基础部件;它们的可重用组合超出了它们的范围。被遗弃的 Tix 包提供了一种机制和它们的集合,称为 "mega-widgets",但这个想法显然没有流行起来。
我按照我的方式按照以下方式(使用模型-视图-展示器设计模式)做到了。
看起来像这样:
import tkinter
from tkinter import ttk
from tkinter import W, E
class View_EditableList(object):
def __init__(self,root,root_row):
""" List with buttons to edit its contents.
:param root: parent widget
:param roow_row: row in `root'. The section takes 2 rows.
"""
self.list = tkinter.Listbox(root, selectmode=tkinter.EXTENDED)
self.list.grid(row=root_row, sticky=(W, E))
root.rowconfigure(root_row, weight=1)
self.frame = ttk.Frame(root)
self.frame.grid(row=root_row+1, sticky=(W, E))
self.add = ttk.Button(self.frame, text="+", width=5)
self.add.grid(row=0, column=1)
self.edit = ttk.Button(self.frame, text="*", width=5)
self.edit.grid(row=0, column=2)
self.del_ = ttk.Button(self.frame, text="-", width=5)
self.del_.grid(row=0, column=3)
self.up = ttk.Button(self.frame, text=u"↑", width=5)
self.up.grid(row=0, column=4)
self.down = ttk.Button(self.frame, text=u"↓", width=5)
self.down.grid(row=0, column=5)
self.frame.grid_columnconfigure(0, weight=1)
self.frame.grid_columnconfigure(6, weight=1)
class Presenter_EditableList(object):
def __init__(self,view,root):
"""
:param view: View_EditableList
:param root: root widget to be used as parent for modal windows
"""
self.root = root
self.view = view
view.add.configure(command=self.add)
view.edit.configure(command=self.edit)
view.del_.configure(command=self.del_)
view.up.configure(command=self.up)
view.down.configure(command=self.down)
def add(self):
w=View_AskText(self.root)
self.root.wait_window(w.top)
if w.value:
self.view.list.insert(self.view.list.size(),w.value)
def edit(self):
l=self.view.list
try:
[index]=l.curselection()
except ValueError:
return
w=View_AskText(self.root,l.get(index))
self.root.wait_window(w.top)
if w.value:
l.delete(index)
l.insert(index,w.value)
def del_(self):
l=self.view.list
try:
[index]=l.curselection()
except ValueError:
return
l.delete(index)
l.select_set(max(index,l.size()-1))
def up(self):
l = self.view.list
try:
[index] = l.curselection()
except ValueError:
return
if index>0:
v = l.get(index)
l.delete(index)
l.insert(index-1,v)
l.select_set(index-1)
def down(self):
l = self.view.list
try:
[index] = l.curselection()
except ValueError:
return
if index<l.size()-1:
v = l.get(index)
l.delete(index)
l.insert(index+1,v)
l.select_set(index+1)
def getlist(self):
return [self.view.list.get(i) for i in range(self.view.list.size())]
def setlist(self,list_):
self.view.list.delete(0,tkinter.END)
for i,v in enumerate(list_):
self.view.list.insert(i,v)
# supplemental class; it's in another file in my actual code
class View_AskText(object):
"""
A simple dialog that asks for a text value.
"""
def __init__(self, master, value=u""):
self.value = None
top = self.top = tkinter.Toplevel(master)
top.grab_set()
self.l = ttk.Label(top, text=u"Value:")
self.l.pack()
self.e = ttk.Entry(top)
self.e.pack()
self.b = ttk.Button(top, text='Ok', command=self.save)
self.b.pack()
if value: self.e.insert(0, value)
self.e.focus_set()
top.bind('<Return>', self.save)
def save(self, *_):
self.value = self.e.get()
self.top.destroy()
root = tkinter.Tk()
view = View_EditableList(root, 5)
Presenter_EditableList(view, root)
root.mainloop()