如何在列表框中移动滚动条?请参阅所附照片,我使用红色箭头进行解释,这是我的代码:
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()
最佳答案
建议将滚动条放在列表框的右侧而不是内部。要实现它:
- 使滚动条成为
frame_listbox
的子级,而不是window
- 将选项
padx=400
从scrollbar.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()
关于python - 如何在 Tkinter 中将滚动条放入列表框内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74345552/