【问题标题】:Tkinter treeview scroll bar not shown properlyTkinter 树视图滚动条未正确显示
【发布时间】:2021-09-27 09:52:06
【问题描述】:

我使用 tkinter 创建了一个带有多个框架的窗口应用程序。在下面的代码 sn-p 框架 frm1 中,我添加了一个可以正常工作的树视图小部件,但是一旦我关联了一个垂直和水平滚动条 - 树视图小部件就会显示缩小到左上角。为什么会这样?

见下方代码sn-p

import tkinter as tk
from tkinter import ttk
from tkinter import BOTTOM, TOP, RIGHT, LEFT, SUNKEN, W, X, Y, NO, VERTICAL, HORIZONTAL, DISABLED, NONE, S, N, E, W

class myApp:
    def __init__(self, wnd):
        self.wnd = wnd
        self.wnd.title(f"My App")
        self.wnd.geometry('1300x700')
        self.wnd.resizable(False, False)

        self.top_frame = tk.Frame(master=self.wnd, width=1300, height=90, highlightcolor="black", highlightbackground="black",bg='yellow')
        self.center = tk.Frame(master=self.wnd, width=1300, height=600, highlightcolor="black", highlightbackground="black",)
        self.status_frame = tk.Frame(master=self.wnd, width=1300, height=22, highlightcolor="black", highlightbackground="black", highlightthickness=1,bg='red')

        self.wnd.grid_rowconfigure(0, weight=1)
        self.wnd.grid_rowconfigure(1, weight=1)
        self.wnd.grid_rowconfigure(2, weight=1)
   
        self.top_frame.grid(row=0, column=0,sticky="n")
        self.center.grid(row=1, column=0, sticky="n")
        self.status_frame.grid(row=2, sticky="nsew")

##############################

        self.center.grid_columnconfigure(0, weight=1)
        self.center.grid_columnconfigure(1, weight=1)
   
        self.ctr_left = tk.Frame(self.center, width=900, height=600, highlightcolor="black", highlightbackground="black", highlightthickness=1)
        self.ctr_right = tk.Frame(self.center, width=400, height=600, highlightcolor="black", highlightbackground="black", highlightthickness=1)
   
        self.ctr_left.grid(row=0, column=0, sticky="nsew")
        self.ctr_right.grid(row=0, column=1, sticky="nsew")
   
###############################       
    
        self.ctr_left.grid_rowconfigure(0, weight=1)
        self.ctr_left.grid_rowconfigure(1, weight=1)
   
        self.frm1 = tk.Frame(self.ctr_left, width=900, height=550, highlightcolor="black", highlightbackground="black", bg='green')
        self.frm2 = tk.Frame(self.ctr_left, width=900, height=50, highlightcolor="black", highlightbackground="black", highlightthickness=1,bg='purple')
   
        self.frm1.grid(row=0, column=0, sticky="nw")
        self.frm2.grid(row=1, column=0, sticky="sw")

################################
   
        self.ctr_right.grid_rowconfigure(0, weight=1)
        self.ctr_right.grid_rowconfigure(1, weight=1)
   
        self.frm3 = tk.Frame(self.ctr_right, width=400, height=550, highlightcolor="black", highlightbackground="black", bg='orange')
        self.frm4 = tk.Frame(self.ctr_right, width=400, height=50, highlightcolor="black", highlightbackground="black", highlightthickness=1,bg='pink')
   
        self.frm3.grid(row=0, column=0, sticky="nw")
        self.frm4.grid(row=1, column=0, sticky="se")
   
    
        #add treeview to frame frm1
        self.style = ttk.Style()
        self.style.theme_use("clam")
        self.style.configure("Treeview",
            background="silver",
            foreground="black",
            fieldbackground="silver")
        self.style.map("Treeview", background=[('selected','green')])
   

        self.my_tree = ttk.Treeview(master=self.frm1, height=26)
   
        # define our columns
        self.my_tree['columns'] = ("Date & Time", "Type", "Log Message")
        self.my_tree.column("#0", width=0, minwidth=0, stretch=NO)
        self.my_tree.column("Date & Time", anchor=W, width=140, minwidth=140)
        self.my_tree.column("Type", anchor=W, width=70, minwidth=70)
        self.my_tree.column("Log Message", anchor=W, width=685, minwidth=400, stretch=True)

        # create headings
        self.my_tree.heading("#0", text="", anchor=W)
        self.my_tree.heading("Date & Time", text="Date & Time", anchor=W)
        self.my_tree.heading("Type", text="Type", anchor=W)
        self.my_tree.heading("Log Message", text="Log Message", anchor=W)
        self.my_tree.grid(row=0, column=0, sticky="nsew")
   
        #add scrollbar to treevew widget my_tree
        self.ytree_scroll = ttk.Scrollbar(master=self.my_tree, orient=VERTICAL, command=self.my_tree.yview)
        self.xtree_scroll = ttk.Scrollbar(master=self.my_tree, orient=HORIZONTAL, command=self.my_tree.xview)       
        self.ytree_scroll.grid(row=0, column=0, sticky="nse")
        self.xtree_scroll.grid(row=0, column=0, sticky="ews")
   
        self.my_tree.configure(yscroll=self.ytree_scroll.set, xscroll=self.xtree_scroll.set)      
    

        #add scrollbar to frame widget frm3
#        self.ytree_scroll1 = ttk.Scrollbar(master=self.frm3, orient=VERTICAL)
#        self.xtree_scroll1 = ttk.Scrollbar(master=self.frm3, orient=HORIZONTAL)       
#        self.ytree_scroll1.grid(row=0, column=0, sticky="nse")
#        self.xtree_scroll1.grid(row=0, column=0, sticky="ews")


if __name__ == "__main__":
    window = tk.Tk()
    application = myApp(window)
    window.mainloop()

【问题讨论】:

    标签: python tkinter treeview


    【解决方案1】:

    您不应该在树视图本身上添加滚动条,而是在同一个父窗口上。所以你应该这样做:

        self.ytree_scroll = ttk.Scrollbar(master=self.frm1, orient=VERTICAL, command=self.my_tree.yview)
        self.xtree_scroll = ttk.Scrollbar(master=self.frm1, orient=HORIZONTAL, command=self.my_tree.xview)       
        self.ytree_scroll.grid(row=0, column=1, sticky="nse")
        self.xtree_scroll.grid(row=1, column=0, columnspan=2, sticky="ews")
    

    之后您应该更改一些内容,因为这会在相邻帧上产生一些空白。

    【讨论】:

      猜你喜欢
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 2013-02-11
      • 1970-01-01
      相关资源
      最近更新 更多