Python tkinter滚动框架,在画布中展开LabelFrame

时间:2012-03-18 21:33:36

标签: python canvas tkinter expand

美好的一天。我有滚动框架的问题。我使用现成的解决方案(http://effbot.org/zone/tkinter-autoscrollbar.htm)

但我不知道如何通过窗口扩展LabelFrame(“kontakty”)?请原谅我的英文

#prevzato z http://effbot.org/zone/tkinter-autoscrollbar.htm

from Tkinter import *

class AutoScrollbar(Scrollbar):
    # a scrollbar that hides itself if it's not needed.  only
    # works if you use the grid geometry manager.
def set(self, lo, hi):
    if float(lo) <= 0.0 and float(hi) >= 1.0:
        # grid_remove is currently missing from Tkinter!
        self.tk.call("grid", "remove", self)
    else:
        self.grid()
    Scrollbar.set(self, lo, hi)
def pack(self, **kw):
    raise TclError, "cannot use pack with this widget"
def place(self, **kw):
    raise TclError, "cannot use place with this widget"

root = Tk()


master = Frame(root)
master.grid(row=0, column=0, sticky=N+S+E+W)


vscrollbar = AutoScrollbar(master)
vscrollbar.grid(row=0, column=1, sticky=N+S)
hscrollbar = AutoScrollbar(master, orient=HORIZONTAL)
hscrollbar.grid(row=1, column=0, sticky=E+W)

master.rowconfigure(0, weight=1)
master.columnconfigure(0, weight=1)

root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)



canvas = Canvas(master,
            yscrollcommand=vscrollbar.set,
            xscrollcommand=hscrollbar.set, bg = "red", width = 200, height = 400)
canvas.grid(row=0, column=0, sticky=N+S+E+W)

canvas.rowconfigure(0, weight=1)
canvas.columnconfigure(0, weight=1)

vscrollbar.config(command=canvas.yview)
hscrollbar.config(command=canvas.xview)

# make the canvas expandable
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

#
# create canvas contents

kontakty = LabelFrame(canvas, width = 600, height =400, text = "Skupina" )
kontakty.grid(row=0, column=0, sticky=N+S+E+W)

kontakty.rowconfigure(0, weight=1)
kontakty.columnconfigure(0, weight=1)



canvas.create_window(0, 0, anchor=NW, window=kontakty)

kontakty.update_idletasks()

canvas.config(scrollregion=canvas.bbox("all"))

root.mainloop()

1 个答案:

答案 0 :(得分:0)

创建为canvas对象的窗口必须告诉它们有多宽,如果你希望它们不是它们的自然大小。

最常见的方法是绑定到画布的<Configure>事件,每次画布大小发生变化时(例如首次绘制时或用户调整窗口大小时)都会调用该事件)。然后,您可以查询画布的大小,然后迭代所有嵌入的小部件,并将其大小设置为画布的宽度(显然,减去所需的任何边框或边距)。