最近用Tk写了个UI,发现TK里面的scrollbar并不像Java里面可以attach到各种组件,只能attach到诸如frame和canvas等少量组件,如果想实现一个带scrollbar的text area或者checkbox list就需要额外实现,而且对于鼠标的滚轮动作默认不感知,也需要额外实现。总之,python的TK开发起来还是挺烦的,没有事件监听,很多地方都需要用TK variable 来实现,扯远了,附上实现了verticalscrollbar和滚轮动作的Frame类,使用时需要注意把child组件的parent指定为这个新类的interior,例如:
调用:
dst_frame = VerticalScrolledFrame(frame, height=500, borderwidth=5, relief="groove")
text_area = ttk.Checkbutton(dst_frame.interior)
class实现:
import Tkinter as tk
import ttk
class VerticalScrolledFrame(tk.Frame):
"""A pure Tkinter scrollable frame that actually works!
* Use the 'interior' attribute to place widgets inside the scrollable frame
* Construct and pack/place/grid normally
* This frame only allows vertical scrolling
"""
def __init__(self, parent, *args, **kw):
tk.Frame.__init__(self, parent, *args, **kw)
# create a canvas object and a vertical scrollbar for scrolling it
vscrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL)
vscrollbar.pack(fill=tk.Y, side=tk.RIGHT, expand=tk.FALSE)
canvas = tk.Canvas(self, bd=1, highlightthickness=1,
yscrollcommand=vscrollbar.set)
canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=tk.TRUE)
vscrollbar.config(command=canvas.yview)
# reset the view
canvas.xview_moveto(0)
canvas.yview_moveto(0)
# create a frame inside the canvas which will be scrolled with it
self.interior = interior = ttk.Frame(canvas)
interior_id = canvas.create_window(0, 0, window=interior,
anchor=tk.NW)
# track changes to the canvas and frame width and sync them,
# also updating the scrollbar
def _configure_interior(event):
# update the scrollbars to match the size of the inner frame
size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
canvas.config(scrollregion="0 0 %s %s" % size)
if interior.winfo_reqwidth() != canvas.winfo_width():
# update the canvas's width to fit the inner frame
canvas.config(width=interior.winfo_reqwidth())
interior.bind('', _configure_interior)
def _configure_canvas(event):
if interior.winfo_reqwidth() != canvas.winfo_width():
# update the inner frame's width to fill the canvas
canvas.itemconfigure(interior_id, width=canvas.winfo_width())
canvas.bind('', _configure_canvas)
def _bound_to_mousewheel(event):
canvas.bind_all("", _on_mousewheel)
canvas.bind('', _bound_to_mousewheel)
def _unbound_to_mousewheel(event):
canvas.unbind_all("")
canvas.bind('', _unbound_to_mousewheel)
def _on_mousewheel(event):
canvas.yview_scroll(-1 * (event.delta / 120), "units")
canvas.bind_all('', _on_mousewheel)