python - 如何在 Tkinter 中将滚动条放入列表框内?

标签 python tkinter

如何在列表框中移动滚动条?请参阅所附照片,我使用红色箭头进行解释,这是我的代码:

from tkinter import *
window = Tk()
window.geometry('500x500')
window.config(bg='#3c3f41')
window.state('zoomed')
frame_listbox = Frame(window,bg='#3c3f41')
frame_listbox.pack(side=LEFT)
listbox = Listbox(frame_listbox,font=('Helvetica',30))
listbox.pack(padx=400)
scrollbar = Scrollbar(window)
scrollbar.pack(side=RIGHT,fill=Y)
scrollbar.config(command=listbox.yview)
listbox.config(yscrollcommand=scrollbar.set)
window.mainloop()

我尝试将滚动条放入列表框中,我希望了解是否可能 enter image description here

最佳答案

建议将滚动条放在列表框的右侧而不是内部。要实现它:

  • 使滚动条成为 frame_listbox 的子级,而不是 window
  • 将选项 padx=400scrollbar.pack(...) 移动到 frame_listbox.pack(...)
  • 将列表框放在左侧
from tkinter import *
window = Tk()
window.geometry('500x500')
window.config(bg='#3c3f41')
window.state('zoomed')
frame_listbox = Frame(window,bg='#3c3f41')
frame_listbox.pack(side=LEFT,padx=400) # added padx=400
listbox = Listbox(frame_listbox,font=('Helvetica',30))
listbox.pack(side=LEFT) # removed padx=400 and added side=LEFT
scrollbar = Scrollbar(frame_listbox) # changed parent to frame_listbox
scrollbar.pack(side=RIGHT,fill=Y)
scrollbar.config(command=listbox.yview)
listbox.config(yscrollcommand=scrollbar.set)
window.mainloop()

enter image description here

关于python - 如何在 Tkinter 中将滚动条放入列表框内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74345552/

相关文章:

python - 使用带有 numpy 矩阵的 Strassen 算法的输出矩阵不正确

python - 使用 Python ElementTree 读取包含多个顶级项目的 XML?

python - 总结直方图中的 Nan

python - tkinter 左键单击 GUI 中的 TAB。如何检测用户何时执行此操作

python - Tkinter 网格管理器

python - 当用户输入有效 URL 或不使用 PYTHON/Flask 时,如何为用户设置代码?

python - 递归闭包(函数发生器)

python - PIL 和 Tkinter,多窗口

python - 创建与 'clam' ttk 主题相同的自定义 ttk 样式(特定于按钮小部件)

python - 如何在 python tkinter 中使窗口处于空闲状态直到按下按钮?