首页 > 解决方案 > 滚动条添加填充 tkinter

问题描述

我试图将滚动条放在 tkinter 列表框中,但每次添加它们时,都会在主窗口的顶部添加大量空间。

以下是我的应用程序中的一些片段。第一个只是生成列表框,正如您从下面的第一个链接图像中看到的那样,结果是一个漂亮的刷新窗口。但是,当我从第二个片段添加滚动条时,窗口顶部会放置大量空间。

#Create a listbox and fill with the data found in a dictionary called "d"
self.dataDisplay = Listbox(root, font = ("helvetica, 14"))
                    
self.subfields = list(d.keys()) #d is a dictionary that is built elsewhere
                
for i in range(len(self.subfields)): #iterate through each dictionary item
      dataType = type(d[self.subfields[i]])
      self.dataDisplay.insert(END, str(helpers.displayHandler(self, self.subfields[i], str(dataType)))) #fill the listbox using a helper function called displayHandler that simply formats the data as a string

self.dataDisplay.pack(fill = "both", expand = True)

上面的代码效果很好,并且与我的应用程序中的其余代码一起,显示了一个如下所示的窗口: No scrollbars, no extra padding

添加以下几行代码以将滚动条放入列表框中,会导致将大量填充添加到窗口顶部。

scrollbarY = Scrollbar(self.dataDisplay, orient=VERTICAL)
scrollbarY.pack(side=RIGHT, fill=Y)
scrollbarX = Scrollbar(self.dataDisplay, orient=HORIZONTAL)
scrollbarX.pack(side=BOTTOM, fill=X)
self.dataDisplay.config(xscrollcommand = scrollbarX.set)
scrollbarY.config(command=self.dataDisplay.yview)
scrollbarX.config(command=self.dataDisplay.xview)

结果如下: 滚动条,额外的填充

我知道无法在我的应用程序中提供所有代码可能会限制可能的答案,但我希望有人至少可以解释为什么简单地将滚动条添加到列表框小部件会影响应用程序其他部分的属性。谢谢!

标签: pythontkinterscrollbar

解决方案


推荐阅读