简体   繁体   中英

Python Tkinter Scrollbar using grid layout

i am tryingto make a simple gui that has two listboxes one list box is inside a scrollbar . i am able to add all these to the window but i cant see the other listbox just the scrollbar. i am using GRID layout

here is my code :

from Tkinter import *
import glob, os

master = Tk()

master.resizable(width=FALSE, height=FALSE)
master.geometry('{}x{}'.format(400, 500))

listbox = Listbox(master)
listbox.grid(row=0,column=0)


listbox.insert(END, "a list entry")

for item in ["one", "two", "three", "four"]:
    listbox.insert(END, item)


data=Listbox(master,bg="grey")
scrollbar = Scrollbar(data, orient=VERTICAL)
data.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=data.yview)
for file in glob.glob("*.*"):
        print(file)
        data.insert(END,file)
        #data.grid(row=0,column=2)

data.grid(row=1, column=2, rowspan=4,
                   columnspan=2, sticky=N+E+S+W)
data.columnconfigure(3, weight=1)
scrollbar.grid(column=2, sticky=N+S)


master.mainloop()

The problem is that you are putting your scrollbar inside the listbox, which is the wrong way to do scrollbars. The problem is, when you use grid to place the scrollbar in the listbox, the listbox will shrink to fit the scrollbar. And since the scrollbar doesn't have a defined type, the listbox shrinks to a very small size.

The solution is to make the scrollbar the child of a frame, not the child of a listbox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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