Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

python - How can I attach a vertical scrollbar to a treeview using Tkinter?

I have been trying to attach a horizontal and vertical scrollbar for the tkinter treeview. In my main application all the data comes from a sql database so I need to be able to scroll through lots of data within a sperate window. I have managed to place the treeview within the child window, however I am still stuck on how to attach scrollbars that work.

Although there is a vertical scrollbar at the moment it doesn't seem to be attached to the treeview and does not scroll through any data that I input.

Is there a way of putting vertical and horizontal scrollbars within my application?

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You connect scrollbars to a Treeview widget the same as you do for any other scrollable widget.

In the case of a vertical scrollbar, the yscrollcommand attribute of the widget needs to be set to the set function of the scrollbar (or something that works just like it). Also, the command attribute of the scrollbar needs to be set to the yview method of the tree (or something that works just like it)

For example:

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

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

Configuring a horizontal scrollbar is similar, though of course it needs to be tied to the xview method rather than the yview method of the tree.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...