简体   繁体   English

调整表单大小会杀死表单滚动

[英]Form re-sizing kills form scrolling

I have an base class which defines my common form functions, including scrolling. 我有一个基类,它定义了我的常见表单功能,包括滚动。

I added form re-sizing capabilities to one of the descendants, and it has killed scrolling. 我向其中一个后代添加了表单大小调整功能,并且它终止了滚动。

In the following extract the form is scrolling. 在下面的摘录中,表单正在滚动。 If you un-comment the one line, it enables the form and fields to be re-sized as I want, but it scrolling is dead. 如果取消注释一行,它将使窗体和字段的大小可以根据我的需要进行调整,但是滚动已停止。

Can someone help please? 有人可以帮忙吗?

from Tkinter import * 
import ttk

class DisplayListWindow(Canvas):

    def __init__(self, parent=None, *args, **kw):
         Canvas.__init__(self,parent, borderwidth=0, *args, **kw)

        self.frame = Frame(self)
        vsb = Scrollbar(parent, orient="vertical", command=self.yview)
        self.configure(yscrollcommand=vsb.set)
        vsb.pack(side="right", fill="y")

        self.create_window((4,4), window=self.frame, anchor="nw", 
                              tags="self.frame")

        self.pack(side="left", fill="both", expand=True)

        self.frame.bind("<Configure>", self.OnFrameConfigure)

        #remaining code in this function is from descendant classes
        for i in range(1, 20, 1):
            ttk.Entry(master=self.frame, style='C.TEntry').grid(row = i, column = 0, sticky = NSEW)
            ttk.Entry(master=self.frame, style='C.TEntry').grid(row = i, column = 1, sticky = NSEW)
            self.frame.columnconfigure(1, weight=1)

        #the following line enables the re-sizing behaviour, but kills scrolling
        #self.frame.pack(side="left", fill="both", expand=True)   # <== problem     

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

DisplayListWindow().mainloop()

If you are using a frame inside a canvas for the purposes of scrolling, you must add it to the canvas using create_window -- you cannot add it using pack or grid . 如果出于滚动目的在画布内部使用框架,则必须使用create_window将其添加到画布中-您不能使用packgrid来添加它。 These two methods (create_window, versus pack or grid) are incompatible with each other. 这两种方法(create_window,vs pack或grid)彼此不兼容。

If you want the frame to resize when the window resizes, you'll have to add code to manually resize it. 如果要在窗口调整大小时调整框架大小,则必须添加代码以手动调整其大小。 You can do this by adjusting the width in your <Configure> callback. 您可以通过在<Configure>回调中调整宽度来实现。

For example: 例如:

def OnFrameConfigure(self, event):
    '''Reset the scroll region to encompass the inner frame'''
    width = self.winfo_width() - 8 # allow room for some padding
    self.itemconfigure("self.frame", width=width)
    self.configure(scrollregion=self.bbox("all"))

Notice how I compute what the width should be by getting the width of the canvas, and then I use that value to set the width of the frame. 注意,如何通过获取画布的宽度来计算宽度,然后使用该值设置框架的宽度。 The height is left to be the natural height of the frame, so the vertical scrollbar should always reflect the complete contents of the frame. 高度保留为框架的自然高度,因此垂直滚动条应始终反映框架的完整内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM