Tkinter滚动条让用户滚动得比他们想要的更多

时间:2018-03-15 00:10:05

标签: python-3.x user-interface tkinter scrollbar

我正在开发一个"细胞"使用python 3.6和tkinter的矩阵,我已经修改了一个滚动的框架类(下面的代码),因为滚动条允许我滚动滚动条中没有剩余的空间。结果是不希望的空间(见下图)。我不习惯使用AutoScrollbar类来滚动滚动条,并且在Windows下工作正常,但在Linux下仍然让我滚动得比它应该更多并且不会变灰。

  • 有什么方法可以阻止tkinter滚动条进一步滚动它们应该吗?

在Windows下: Grays out 在Linux下: Not grayed out but still let me scroll

import tkinter as tk   
from tkinter import ttk

class AutoScrollbar(tk.Scrollbar):
    def set(self, lo, hi):
        if float(lo) <= 0.0 and float(hi) >= 1.0:
            tk.Scrollbar.set(self, 0.0, 1.0)
        else:
            tk.Scrollbar.set(self, lo, hi)

class ScrolledFrame(tk.Frame):
    def __init__(self, parent, *args, **kw):
        tk.Frame.__init__(self, parent, *args, **kw)            
        vscrollbar = AutoScrollbar(self, orient=tk.VERTICAL)
        vscrollbar.pack(fill=tk.Y, side=tk.RIGHT, expand=False)
        hscrollbar = AutoScrollbar(self, orient=tk.HORIZONTAL)
        hscrollbar.pack(fill=tk.X, side=tk.BOTTOM, expand=False)       
        canvas = tk.Canvas(self, bd=0, highlightthickness=0,
                            yscrollcommand=vscrollbar.set,
                            xscrollcommand=hscrollbar.set,
                            bg ='gray')
        canvas.pack(fill=tk.BOTH, expand=True)
        vscrollbar.config(command=canvas.yview)
        hscrollbar.config(command=canvas.xview)
        # 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 = tk.Frame(canvas)
        interior_id = canvas.create_window(0, 0, window=interior,
                                           anchor=tk.NW)
        # detect changes on interior size
        def _configure_interior(event):
            # adjust size of scroll region
            size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
            #print('size interior = canvas: '+str(size))
            canvas.config(scrollregion="0 0 %s %s" % size)
        interior.bind('<Configure>', _configure_interior)

0 个答案:

没有答案