如何在树视图中使用水平滚动,这里我使用树视图制作表格 [英] How to use horizontal scrolling in treeview,here i use tree view to make a table
本文介绍了如何在树视图中使用水平滚动,这里我使用树视图制作表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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 的图像(像一张桌子).我的 tkinter 窗口截图
From the above code i created a table out of a treeview, my y-axis scroll is working perfect, but i am not able to work on the xscrollbar. kindly help me out thank you. I am attaching one image of the resultant Treeview(table like one). screeshot of my tkinter window
推荐答案
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
此功能中的一些编辑帮助我完成了,感谢那些尝试过的人.我使用了树视图的一个额外参数,即最小宽度.宽度和最小宽度的差异将使我们能够解决这个问题.
some edit in this function helped me through, thank you for those who tried. I used an extra parameter of the treeview i.e. , the minwidth. having a difference in width and min width will allow us to go through this issue.
这篇关于如何在树视图中使用水平滚动,这里我使用树视图制作表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文