如何在treeview中使用水平滚动,在这里我使用树视图来制作一张表

如何在treeview中使用水平滚动,在这里我使用树视图来制作一张表

问题描述:

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如何在treeview中使用水平滚动,在这里我使用树视图来制作一张表

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 

在这个函数的一些编辑帮助的性质我通过,谢谢你的尝试。 我使用了treeview的额外参数,即最小宽度。在宽度和最小宽度上有差异的 将使我们能够经历这个问题。

根据截图,它看起来像你的表没有足够的空间提供的根窗口。 滚动条可能是有的,但不可见,因为该表在rightside切割,所以你需要适应您使用的表的根窗口的几何管理

+0

我有没有见过一个3分或4个列其中的arent可见多个列,问题是图像中的滚动条显示。不是没有显示的。水平滚动条是不工作的。超出边界的情况正在发挥作用。 –

+0

你能提供一种限制树视图水平尺寸的方法吗?其实我认为这可以解决问题,我只能找到大小的列 –

可以水平滚动树状,rezize的colomns(在运行时,拖动到“关闭屏幕”的右侧)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)