滚动条添加填充tkinter

2024-06-11 10:52:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将滚动条放在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)

结果如下: Scrollbars, extra padding

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


Tags: the代码selfconfig应用程序dictionary空间fill