停止tkinter框架被推离屏幕

时间:2017-07-31 18:14:45

标签: python python-3.x tkinter scrollbar frame

我有一个包含3个框架的tk窗口,左侧的工作方式类似于树状结构,其标签将使用.grid()grid_remove()添加和删除,右框架将显示有关这些项目的信息,以及将按住按钮的底部框架。如果我在左框架(lframe)上展开太多项目,则会将底部框架(bframe)推出视图。我希望底部框架在到达窗口边缘时停止,并且lframe的滚动条用于查看lframe的项目。

这是我看到的一个简单例子。单击该按钮时,将创建更多标签窗口小部件,并将bframe推出视图。

from tkinter import *

main = Tk()

canvas = Canvas(main)
canvas.grid(row=0, column=0)
canvas.configure(scrollregion=canvas.bbox("all"))
lframe = Frame(canvas)
lframe.grid(row=0, column=1)


sbar = Scrollbar(canvas)
sbar.config(command=canvas.yview)
canvas.config(yscrollcommand=sbar.set)
sbar.grid(row=0, column=0, sticky=NSEW)

rframe = Frame(main)
rframe.grid(row=0, column=1, sticky=N)

bframe = Frame(main)
bframe.grid(row=1, column=1)


cnt = 1
def addmore():
    global cnt

    test = Label(lframe, text='this \nis \nthe \nLEFT \nframe\nthis \nis \nthe \nLEFT \nframe\nthis \nis \nthe \nLEFT \nframe\nthis \nis \nthe \nLEFT \nframe\nthis \nis \nthe \nLEFT \nframe\n')
    test.grid(row=cnt, column=0)
    cnt += 1
    lframe.update()


llbl = Label(lframe, text='this \nis \nthe \nLEFT \nframe')
llbl.grid()

rlbl = Label(rframe, text='this is the RIGHT frame')
rlbl.grid()
btn = Button(rframe, text='click me', command=addmore)
btn.grid(row=1, column=0, sticky=N)
blbl = Label(bframe, text='this is the BOTTOM frame', bg="yellow")
blbl.grid(row=0)


main.mainloop()

问题1:  如何阻止bframe推离屏幕?我希望它在到达main底部时停止,lframe中的标签开始推出bframe后面的视图。然后,用户可以使用滚动条查看lframe中的其他项目。

问题2:  我不知道我的滚动条是否配置正确,直到我可以让lframe停止推离屏幕。如果设置不正确,我需要更改什么?

1 个答案:

答案 0 :(得分:1)

以下是如何使用主框架中的权重设置此滚动画布的示例。它不完美,但它应该足以帮助。

import tkinter as tk

class Example(tk.Frame):
    def __init__(self):

        tk.Frame.__init__(self)

        self.canvas = tk.Canvas(self, borderwidth=0, width = 50, height = 100)
        self.lframe = tk.Frame(self.canvas, width=10)
        self.sbar = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.sbar.set)

        self.sbar.grid(row=0, column=0, rowspan=2, sticky="nsw")
        self.canvas.grid(row=0, column=1, rowspan=2, sticky="nsew")
        self.canvas.create_window((2,2), window=self.lframe, anchor="ne", 
                                  tags="self.lframe")

        self.lframe.bind("<Configure>", self.onFrameConfigure)
        self.rframe = tk.Frame(self)
        self.rframe.grid(row=0, column=2, sticky="n")

        self.bframe = tk.Frame(self)
        self.bframe.grid(row=1, column=2)

        llbl = tk.Label(self.lframe, text='this \nis \nthe \nLEFT \nframe')
        llbl.grid(row = 0, column = 0)

        rlbl = tk.Label(self.rframe, text='this is the RIGHT frame')
        rlbl.grid(row = 0, column = 0)
        btn = tk.Button(self.rframe, text='click me', command=self.addmore)
        btn.grid(row=1, column=0, sticky="n")
        blbl = tk.Label(self.bframe, text='this is the BOTTOM frame', bg="yellow")
        blbl.grid(row = 2, column = 0, sticky = "s")

        self.bframe.rowconfigure(1, weight = 1)
        self.bframe.rowconfigure(2, weight = 0)
        self.rowconfigure(0, weight = 1)
        self.rowconfigure(1, weight = 1)
        self.columnconfigure(0, weight = 0)
        self.columnconfigure(1, weight = 1)
        self.columnconfigure(2, weight = 0)

        self.cnt = 1

    def addmore(self):
        test = tk.Label(self.lframe, text='this \nis \nthe \nLEFT \nframe\nthis \nis \nthe \nLEFT \nframe\nthis \nis \nthe \nLEFT \nframe\nthis \nis \nthe \nLEFT \nframe\nthis \nis \nthe \nLEFT \nframe\n')
        test.grid(row=self.cnt, column=0)
        self.cnt += 1

    def onFrameConfigure(self, event):
        '''Reset the scroll region to encompass the inner frame'''
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

if __name__ == "__main__":
    root=tk.Tk()
    Example().grid(row=0, column=0)
    root.mainloop()