简体   繁体   English

TkInter ScrollBar不适用于ListBox

[英]TkInter ScrollBar does not work with ListBox

Here is my code for a ListBox and ScrollBar. 这是我的ListBox和ScrollBar的代码。 The ScrollBar is showing at the right side of my frame but is disabled and not working when the items is ListBox exceeds its area: 滚动条显示在我的框架的右侧,但当ListBox项超出其区域时被禁用并且不起作用:

frameLeft = Frame(self, relief = GROOVE, borderwidth = 4)
frameLeft.pack(side = LEFT, fill = Y, padx = 2, pady = 2)

scrollbar = Scrollbar(frameLeft)
scrollbar.pack(side = RIGHT, fill = Y)
lbModules = Listbox(frameLeft, relief = GROOVE, borderwidth = 2.5, yscrollcommand = scrollbar.set)
lbModules.pack(side = LEFT, fill = BOTH)
scrollbar.config(command = lbModules.yview)

for m in moduleList:
    lbModules.insert(END, Checkbutton(lbModules, text = m).pack(side = TOP, fill = X, padx = 2, pady = 2))

** moduleList is a list of strings ** Also if i add only the text to the ListBox, ScrollBar works but if i add the CheckButton, ScrollBar does not work. ** moduleList是字符串列表**同样,如果我仅将文本添加到ListBox,则ScrollBar起作用,但是如果我添加CheckButton,则ScrollBar不起作用。

You can only insert strings to the listbox, and the scrollbar only affects things inserted into the listbox with the .insert() method. 您只能将字符串插入列表框,并且滚动条仅会影响使用.insert()方法插入列表框的内容。 You simply cannot insert checkbuttons into a listbox. 您根本无法将复选框插入列表框。

In your code you're attempting to pack a checkbutton into a listbox, but that's not the same as inserting a checkbox in a listbox. 在您的代码中,您尝试将一个检查按钮pack到一个列表框中,但这与在列表框中inserting一个复选框不同。 While you can technically pack some other widget into a listbox, it won't be "in" the listbox and won't be scrollable. 从技术上讲,您可以将其他一些小部件打包到列表框中,但它不会“出现在”列表框中,也不会滚动。

In your specific case, the reason you see nothing -- not even the checkbox names -- is because you're inserting None for each checkbutton. 在您的特定情况下,您什么都看不到的原因-甚至没有复选框名称-的原因是,您为每个复选框插入了None。 In python, when you do foo().bar() , the end result is the result of the last command, bar() . 在python中,当您执行foo().bar() ,最终结果是最后一条命令bar() So, when you do Checkbutton(...).pack(...) , the result is whatever pack(...) returns, which is always None. 因此,当您执行Checkbutton(...).pack(...) ,结果就是pack(...)返回的结果,始终为None。 So, lbModules.insert(Checkbutton(...).pack(...)) is the same as doing lbModules.insert(None) . 因此, lbModules.insert(Checkbutton(...).pack(...))与执行lbModules.insert(None)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM