小编典典

如何使用Tkinter将垂直滚动条附加到树视图?

python

我一直在尝试为tkinter
treeview附加水平和垂直滚动条。在我的主要应用程序中,所有数据均来自sql数据库,因此我需要能够在一个适当的窗口中滚动浏览大量数据。我已经设法将Treeview放置在子窗口中,但是我仍然对如何附加有效的滚动条感到困惑。

尽管目前有一个垂直滚动条,但它似乎没有附加到树形视图上,也不会滚动浏览我输入的任何数据。

有没有一种方法可以在应用程序中放置垂直和水平滚动条?

import Tkinter as tk
import os
import sys
import re
import ttk
from Tkinter import *
import tkFont


class application(tk.Tk):

    def __init__(self, *args, **kwargs):
            tk.Tk.__init__(self, *args, **kwargs)
            container = tk.Frame(self)
            container.pack(side="top", fill="both", expand = True)
            container.grid_rowconfigure(0, weight=1)
            container.grid_columnconfigure(0, weight=1)
            self.frames = {}

            for F in (app_one, app_two):
                frame = F(container, self)
                self.frames[F] = frame
                frame.grid(row=0, column=0, sticky="nsew")
            self.show_frame(app_one)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class app_one(tk.Frame):

    def __init__(self, parent, controller):
            tk.Frame.__init__(self,parent)
            button = ttk.Button(self, text="Page One", command=lambda: controller.show_frame(app_two))
            button.pack()


class app_two(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller=controller
        self.msgText = tk.StringVar()
        button = ttk.Button(self, text="Open", command= self.Child_Window)
        button.pack()

    def Child_Window(self):
        win2 = Toplevel()
        new_element_header=['1st','2nd','3rd','4th']
        treeScroll = ttk.Scrollbar(win2)
        treeScroll.pack(side=RIGHT, fill=Y)
        tree = ttk.Treeview(win2,columns=new_element_header, show="headings", yscrollcommand = treeScroll)
        tree.heading("1st", text="1st")
        tree.heading("2nd", text="2nd")
        tree.heading("3rd", text="3rd")
        tree.heading("4th", text="4th")
        tree.insert("" , 0,    text="Line 1", values=("1A","1b"))
        tree.insert("" , 0,    text="Line 2", values=("1A","1b"))
        tree.insert("" , 0,    text="Line 3", values=("1A","1b"))
        tree.insert("" , 0,    text="Line 4", values=("1A","1b"))
        tree.insert("" , 0,    text="Line 5", values=("1A","1b"))
        tree.insert("" , 0,    text="Line 6", values=("1A","1b"))
        tree.insert("" , 0,    text="Line 7", values=("1A","1b"))

        tree.pack(side=LEFT, fill=BOTH)
        treeScroll.config(command=tree.yview)


app = application()
app.wm_geometry("420x200")
app.wm_title("Test")
app.mainloop()

阅读 261

收藏
2021-01-20

共1个答案

小编典典

将滚动条连接到Treeview窗口小部件,就像将其连接到任何其他可滚动窗口小部件一样。

对于垂直滚动条,yscrollcommand需要将小部件的属性设置为set滚动条的功能(或类似功能的东西)。同样,command需要将滚动条的属性设置为yview树的方法(或类似的方法)

例如:

treeScroll = ttk.Scrollbar(...)
tree = ttk.Treeview(...)

treeScroll.configure(command=tree.yview)
tree.configure(yscrollcommand=treeScroll.set)

配置水平滚动条类似,尽管当然需要将其绑定到xview方法而不是yview树的方法。

2021-01-20