如何制作一个可滚动的列表框?tkinter python

wgx48brx  于 2022-12-21  发布在  Python
关注(0)|答案(2)|浏览(147)

我想创建一个自动隐藏滚动条的列表框。我一直在搜索并找到了一个类的例子;当我摆弄它的时候,滚动条并没有像预期的那样附着在右边。
这是密码:

from tkinter import *

class AutoScrollbar(Scrollbar):
    # a scrollbar that hides itself if it's not needed.  only
    # works if you use the pack geometry manager.
    def set(self, lo, hi):
        if float(lo) <= 0.0 and float(hi) >= 1.0:
            self.pack_forget()
        else:
            if self.cget("orient") == HORIZONTAL:
                self.pack(fill=X)
            else:
                self.pack(fill=Y)
        Scrollbar.set(self, lo, hi)

    def grid(self, **kw):
        raise (TclError, "cannot use grid with this widget")

    def place(self, **kw):
        raise (TclError, "cannot use place with this widget")

class Window1(Tk):
    def __init__(self):
        super().__init__()

        self.list_frame = Frame(self)
        self.list_frame.pack(fill=BOTH, expand=1)

        self.my_scrollbar = AutoScrollbar(self.list_frame, orient=VERTICAL)

        self.box1 = Listbox(self.list_frame, bg='black', fg='white',
                            width=60,
                            selectforeground='black',
                            yscrollcommand=self.my_scrollbar.set,
                            selectmode=EXTENDED)

        self.my_scrollbar.config(command=self.box1.yview)
        self.my_scrollbar.pack(side=RIGHT, fill=Y, pady=20)

        self.box1.pack(pady=20, fill=BOTH, expand=1)

        for num in range(15):
            self.box1.insert(END, num)

if __name__ == '__main__':
    w = Window1()
    w.mainloop()

这是我运行代码时的第一个视图:

当我展开窗口时,滚动条正确消失:

但是,当我最小化窗口时,滚动条又出现了,但位置不正确,形状也很奇怪:

rhfm7lfc

rhfm7lfc1#

因为您没有在AutoScrollbar.set()中指定pack()side选项,所以默认为TOP,并且您忘记在self.box1.pack(...)中指定side
以下是修复问题的修改代码:

from tkinter import *

class AutoScrollbar(Scrollbar):
    # a scrollbar that hides itself if it's not needed.  only
    # works if you use the pack geometry manager.
    def set(self, lo, hi):
        if float(lo) <= 0.0 and float(hi) >= 1.0:
            self.pack_forget()
        else:
            if self.cget("orient") == HORIZONTAL:
                self.pack(fill=X, side=BOTTOM) # added side option
            else:
                self.pack(fill=Y, side=RIGHT)  # added side option
        super().set(lo, hi)

    def grid(self, **kw):
        raise (TclError, "cannot use grid with this widget")

    def place(self, **kw):
        raise (TclError, "cannot use place with this widget")

class Window1(Tk):
    def __init__(self):
        super().__init__()

        self.list_frame = Frame(self, pady=20) # added pady=20
        self.list_frame.pack(fill=BOTH, expand=1)

        self.my_scrollbar = AutoScrollbar(self.list_frame, orient=VERTICAL)

        self.box1 = Listbox(self.list_frame, bg='black', fg='white',
                            width=60,
                            selectforeground='black',
                            yscrollcommand=self.my_scrollbar.set,
                            selectmode=EXTENDED)

        self.my_scrollbar.config(command=self.box1.yview)
        self.my_scrollbar.pack(side=RIGHT, fill=Y) # removed pady=20

        self.box1.pack(fill=BOTH, expand=1, side=LEFT) # added side=LEFT and removed pady=20

        for num in range(15):
            self.box1.insert(END, num)

if __name__ == '__main__':
    w = Window1()
    w.mainloop()
ktecyv1j

ktecyv1j2#

@David 04。您的代码正在工作。您不能使用小于15的值。除非您设置几何或可调整大小。应该可以解决您的问题。
更改此内容:

for num in range(15):
            self.box1.insert(END, num)

致:

for num in range(105):
            self.box1.insert(END, num)

你会得到滚动调整大小。

相关问题