Python 如何将滚动条添加到此列表框

Python 如何将滚动条添加到此列表框,python,tkinter,python-3.6,Python,Tkinter,Python 3.6,Python V3.6,Windows 7,初学者1个月 我正在研究如何将滚动条添加到 使用tkinter的列表框。 谁能告诉我我做错了什么吗 我已经尝试从这里实现一个类似的答案:但我也无法让它工作 #listbox window edit_space = Listbox( master = top, selectmode = 'single', width = 53, height = 10, fg="blue") edit

Python V3.6,Windows 7,初学者1个月

我正在研究如何将滚动条添加到 使用tkinter的列表框。
谁能告诉我我做错了什么吗

我已经尝试从这里实现一个类似的答案:但我也无法让它工作

    #listbox window
    edit_space = Listbox(
    master = top,
    selectmode   = 'single',
    width  = 53,
    height = 10,
    fg="blue")
    edit_space.place(x=10, y=130)
    edit_space.bind('<Double-1>', onselect)

    # add scrollbar to listbox
    #not working
    scrollbar = Scrollbar(edit_space)
    edit_space.config(yscrollcommand=scrollbar.set)
    scrollbar.config(command=edit_space.yview)
#列表框窗口
编辑空间=列表框(
大师=顶尖,
selectmode='single',
宽度=53,
高度=10,
fg=“蓝色”)
编辑空间位置(x=10,y=130)
编辑_space.bind(“”,onselect)
#将滚动条添加到列表框
#不起作用
滚动条=滚动条(编辑\u空间)
编辑_space.config(yscrollcommand=scrollbar.set)
scrollbar.config(命令=edit_space.yview)

您的主要问题是您没有告诉Tkinter使用
.pack
.grid
.place
显示您的滚动条。但是,与其将列表框和滚动条放在窗口中,不如将它们放在一个框架中,这样您就可以将它们视为一个单元。下面是一个源于您的代码的简短演示。我使用的是
pack
布局管理器,而不是
.place
,因为它更容易使用,也更灵活

import tkinter as tk

top = tk.Tk()
top.title('Listbox demo')

def onselect(event):
    # Display the current selection
    idx = edit_space.curselection()[0]
    print(idx, edit_space.get(idx))

# Make a Frame to hold the Listbox and its Scrollbar
frame = tk.Frame(top)
frame.pack()

# Add the listbox
edit_space = tk.Listbox(top, selectmode='single', width=20, height=10, fg="blue")
edit_space.bind('<Double-1>', onselect)

# Add the Scrollbar
scrollbar = tk.Scrollbar(top)

# Pack the Scrollbar first so that it doesn't disappear 
# when the window width is small
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
edit_space.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)

# Connect the Scrollbar to the Listbox
scrollbar.config(command=edit_space.yview)
edit_space.config(yscrollcommand=scrollbar.set)

# Put some data into the Listbox
for i, c in enumerate('abcdefghijklmnopqrstuvwxyz'):
    edit_space.insert(tk.END, '{:2}: {}'.format(i, 3 * c))

top.mainloop()
将tkinter作为tk导入
top=tk.tk()
top.title('Listbox demo')
def onselect(事件):
#显示当前选择
idx=edit_space.curselection()[0]
打印(idx,编辑_space.get(idx))
#制作一个框架以容纳列表框及其滚动条
框架=传统框架(顶部)
frame.pack()
#添加列表框
编辑_space=tk.Listbox(顶部,选择mode='single',宽度=20,高度=10,fg=“蓝色”)
编辑_space.bind(“”,onselect)
#添加滚动条
滚动条=tk.滚动条(顶部)
#首先打包滚动条,使其不会消失
#当窗口宽度较小时
滚动条.pack(side=tk.RIGHT,fill=tk.Y)
编辑_space.pack(side=tk.RIGHT,fill=tk.BOTH,expand=True)
#将滚动条连接到列表框
scrollbar.config(命令=edit_space.yview)
编辑_space.config(yscrollcommand=scrollbar.set)
#将一些数据放入列表框
对于枚举中的i,c('abcdefghijklmnopqrstuvwxyz'):
编辑_space.insert(tk.END,'{:2}:{}'。格式(i,3*c))
top.mainloop()

yes tkinter,我更新了问题以反映这一点,并再次缩进了代码。您的代码确实应该是a,这样我们就可以运行它并重现您的问题。但我想我可以找到问题的原因。以下是完整的源代码:程序在驱动器或文件夹和子目录中搜索.txt文件,并在其中搜索用户搜索词。下面是程序运行的截图:您应该将原始问题编辑为最小和完整,而不是在评论中添加链接。我们的目标,就像帮助你得到问题的答案一样,是形成一个问题,让未来的读者能够识别出与他们的问题相似的问题。谢谢PM。我将尝试看看是否可以将其与我的代码集成。