我正在尝试向我的tkinter窗口添加一个滚动条,该滚动条保持原位并滚动浏览一个框架。但是,每当我启动该应用程序时,滚动条就会变灰。这是我的代码的相关摘录:
self.bigFrame = ttk.Frame(self,width = 1080,height = 500)
self.hsb = tk.Scrollbar(self.bigFrame,orient = "horizontal")
self.hsb.pack(side = TOP, fill = X)
self.treeCanvas = tk.Canvas(self.bigFrame,width = 1080,height = 500,xscrollcommand = self.hsb.set)
self.treeFrame = tk.Frame(self.treeCanvas,width = 1080,height = 500)
self.treeCanvas.create_window((0,0),window=self.treeFrame,anchor = 'nw')
self.treeCanvas.config(scrollregion = self.treeCanvas.bbox("all"))
self.treeCanvas.pack(side = BOTTOM,fill = X)
self.treeFrame.pack(side = BOTTOM,fill = X)
self.hsb.config(command = self.treeCanvas.xview)
self.tree = ttk.Treeview(self.treeFrame,selectmode='browse',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')
self.tree.pack(side = BOTTOM, fill = X)
self.bigFrame.grid(row = 11,column = 0,columnspan = 100,pady=(5,0),sticky = 'nw')
self.bigFrame.grid_rowconfigure(0,weight = 1)
self.bigFrame.grid_columnconfigure(0,weight = 1)
self.bigFrame.grid_propagate(False)
我确定这是行顺序的问题,但是我尝试过的所有命令都没有起作用。
答案 0 :(得分:0)
滚动条显示为灰色的原因是因为缺少此行:
self.treeFrame.update_idletasks()
下面在代码的其余部分中实现。
...
self.treeCanvas.create_window((0,0),window=self.treeFrame,anchor = 'nw')
# add this
self.treeFrame.update_idletasks()
bbox = self.treeCanvas.bbox(tk.ALL)
# Update this
self.treeCanvas.config(scrollregion=bbox, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT)
self.treeCanvas.pack(side = BOTTOM,fill = X)
self.treeFrame.pack(side = BOTTOM,fill = X)
...
希望这个答案会有所帮助,如果不能的话,您可以尝试the last answer from this question,它对我很有帮助。