【问题标题】:How to use horizontal scrolling in treeview,here i use tree view to make a table如何在树视图中使用水平滚动,这里我使用树视图制作表格
【发布时间】:2018-04-01 07:30:59
【问题描述】:
class table(Frame):

def __init__(self, parent,headings=None,data=None):
    Frame.__init__(self, parent,relief='ridge')
    self.parent=parent
    self.headings=headings
    self.data = data
    self.CreateUI(self.headings)
    self.LoadTable(self.data)


    self.yscrollbar = Scrollbar(self.parent,orient=VERTICAL)
    self.yscrollbar.grid(row=0,column=1,sticky = (N,S,W,E))
    self.yscrollbar.config(command=self.treeview.yview)
    self.treeview.config(yscrollcommand=self.yscrollbar.set)

    self.xscrollbar = Scrollbar(self.parent,orient=HORIZONTAL)
    self.xscrollbar.grid(row=1,column=0,sticky = (N,S,W,E))
    self.treeview.config(xscrollcommand=self.xscrollbar.set)
    self.xscrollbar.config(command=self.treeview.xview)

    self.grid(row=0,column=0)
def CreateUI(self,headings):
    tv = Treeview(self,height=20)
    if(headings==None):
        tv['columns'] = ('starttime', 'endtime', 'status')
    else:
        tv['columns'] = headings[1:]
        tv.heading("#0", text=headings[0], anchor='w')
        tv.column("#0", anchor="w")
        for i in headings[1:]:
            tv.heading(i, text=i)
            tv.column(i, anchor='center')
    tv.grid(sticky = (N,S,W,E))
    self.treeview = tv

def LoadTable(self,data):
    if(data==None):
        for i in range(100):
            self.treeview.insert('', 'end', text="first", values=('sdfa','asdfasd0','asdfasdf'))
            self.treeview.insert('', 'end', text="Second", values=('sdfa','asdfasd0','asdfasdf'))
            self.treeview.insert('', 'end', text="third", values=('sdfa','asdfasd0','asdfasdf'))
    else:
        for line in data:
            self.treeview.insert('', 'end', text=line[0], values=line[1:])

从上面的代码中,我从树视图中创建了一个表格,我的 y 轴滚动工作完美,但我无法在 xscrollbar 上工作。请帮帮我谢谢。我附上了一张生成的 Treeview 的图像(像一张一样的表格)。 screeshot of my tkinter window

【问题讨论】:

    标签: python tkinter treeview scrollbar


    【解决方案1】:

    根据屏幕截图,您的表格似乎没有根窗口提供的足够空间。 滚动条可能在那里,但由于表格在右侧被切割而不可见,因此您需要调整用于表格根窗口的几何管理器的属性

    【讨论】:

    • 我有更多看不到的列,例如 3 或 4 个不可见的列,问题是图像中显示的滚动条。不是未显示的那个。水平滚动条是不工作的。超出边界的垂直工作正常。
    • 你能提供一种方法来限制树视图的水平尺寸吗?实际上我认为这可以解决问题,我只能找到列的大小
    【解决方案2】:
    def CreateUI(self,headings):
        tv = Treeview(self,height=20)
        if(headings==None):
            tv['columns'] = ('starttime', 'endtime', 'status')
        else:
            tv['columns'] = headings[1:]
            tv.heading("#0", text=headings[0], anchor='w')
        --> tv.column("#0", anchor="center",width=100,minwidth=100)
            for i in headings[1:]:
                tv.heading(i, text=i)
            --> tv.column(i, anchor='center',width=90,minwidth=100)##
        tv.pack(expand=Y)
        self.treeview = tv
    

    此功能中的一些编辑帮助我完成了,感谢那些尝试过的人。 我使用了树视图的一个额外参数,即最小宽度。 宽度和最小宽度的不同将使我们能够解决这个问题。

    【讨论】:

      【解决方案3】:

      您可以水平滚动树视图,调整列的大小(在运行时,拖动到右侧的“屏幕外”)激活 xscrollbar,或者 调整代码中列的大小(更大)在创建滚动条和树视图后使用事件,例如按下按钮(例如当树视图被填充时),否则 '.xview' 什么也检测不到

      vsbx = tkinter.Scrollbar(root_search_stock, orient="horizontal")
      vsbx.place(x= 40, y = 550, width = 1000)
      
      
      tree = tkinter.ttk.Treeview(root_search_stock,\
                                 columns=column_names,yscrollcommand=vsby.set,xscrollcommand=vsbx.set)
      tree.place(x = 50, y = 300)
      
      
      vsbx.config(command = tree.xview)
      

      【讨论】:

        猜你喜欢
        • 2018-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-03
        • 1970-01-01
        • 2021-06-08
        • 2011-11-12
        相关资源
        最近更新 更多