设置滚动条以使用树状视图

时间:2018-12-27 17:06:44

标签: python python-3.x tkinter treeview tkinter-layout

我试图在此处向树中添加滚动条,以使数据在用户的视野内,因为树在屏幕外延伸(窗口限制为1280x720)。但是,滚动条不会将其移动。这是代码:

self.treeFrame = ttk.Frame(self)
    self.treeFrame.grid(row = 12,column = 0,columnspan = 1000,rowspan = 100)



    self.tree = ttk.Treeview(self.treeFrame,height = 100, columns = ('name','purchaseprice','previousprices','listingprice','buyingformat','postage','fees','potprofit','offers','viewcount','sold','offertaken','username','dispatch','delivered','returned','relist','feedback'))
    self.tree.heading('#0',text = 'saleID',anchor = 'w')
    self.tree.heading('name',text = "Item Name",anchor = 'w')
    self.tree.heading('purchaseprice',text = "Purchase Price",anchor = 'w')
    self.tree.heading('previousprices',text = "Previous Prices",anchor = 'w')
    self.tree.heading('listingprice',text = "Listing Price", anchor = 'w')
    self.tree.heading('buyingformat',text = "Buying Format",anchor = 'w')
    self.tree.heading('postage',text = "Postage",anchor = 'w')
    self.tree.heading('fees',text = "Fees",anchor = 'w')
    self.tree.heading('potprofit',text = "Potential Profit",anchor = 'w')
    self.tree.heading('offers',text = "Best Offer",anchor = 'w')
    self.tree.heading('viewcount',text = "Viewcount",anchor = 'w')
    self.tree.heading('sold',text = "Sold?",anchor = 'w')
    self.tree.heading('offertaken',text = "Offer Taken?",anchor = 'w')
    self.tree.heading('username',text = "Username",anchor = 'w')
    self.tree.heading('dispatch',text = "Dispatched?",anchor = 'w')
    self.tree.heading('delivered',text = "Delivered?",anchor = 'w')
    self.tree.heading('returned',text = "Returned?",anchor = 'w')
    self.tree.heading('relist',text = "Relisted?",anchor = 'w')
    self.tree.heading('feedback',text = "Feedback",anchor = 'w')

    hsb = ttk.Scrollbar(self,orient = "horizontal")
    self.tree.grid(row = 14,column = 0,sticky ='nsew')
    hsb.grid(row = 11,column = 0,columnspan = 10,sticky = 'ew')
    hsb.config(command = self.tree.xview)

有人知道我将如何限制树的大小,以便使其进入可以在窗口范围内滚动的位置?

1 个答案:

答案 0 :(得分:0)

需要告知滚动条,当用户移动滚动条时要运行什么命令,而小部件需要知道在滚动时要运行哪个命令。您什么都不做。

例如,在您的代码中,您需要执行以下两项操作:

hsb.configure(command=self.tree.xview)
self.tree.configure(xscrollcommand=hsb.set)