Home > database >  Why scrollbar won't show on listbox?
Why scrollbar won't show on listbox?

Time:05-22

I just want put scrollbar to listbox. I try to do it like this ;

import tkinter as tk
from tkinter import ttk, Label, Frame, Entry, Button, Listbox, 
Scrollbar, END, VERTICAL, RIGHT, Y
from PIL import Image, ImageTk

class Page2(tk.Frame):
    
    def __init__(self, parent, controller):
        
       tk.Frame.__init__(self, parent)
       ascrollbar = Scrollbar(self, orient=VERTICAL)
       
       diseaseBox = Listbox(self, width=50, height=300, yscrollcommand = ascrollbar.set)
       diseaseBox.grid(row=1, column = 1, padx = 100, pady = 300)
       
       ascrollbar.config(command = diseaseBox.yview)
       ascrollbar.grid(row=1, column=1)
       

       diseaseList = ["one", "two" , "three"]
       
       if diseaseBox is not None:
           for item in diseaseList:
               diseaseBox.insert(END,item)

But scrollbar won't show. I try to use pack() method but i am getting this error

cannot use geometry manager grid inside .!frame.!page2 which already has slaves managed by pack

How can I solve this?

CodePudding user response:

Your scrollbar and your listbox are in the same place on your grid.

Edit 1 : Adding some code I think with this lines of codes it's will works

diseaseBox = Listbox(self, width=50, height=300, yscrollcommand = ascrollbar.set)
diseaseBox.grid(row=1, column = 1, padx = 100, pady = 300)
       
ascrollbar.config(command = diseaseBox.yview)
ascrollbar.grid(row=1, column=2)

Edit 2 : if is not a problem of grid maybe configure and the making of the scrollbar not good

ascrollbar = Scrollbar(self, orient=VERTICAL,command=diseaseBox.yview)
       
diseaseBox = Listbox(self, width=50, height=300)
diseaseBox.grid(row=1, column = 1, padx = 100, pady = 300)
diseaseBox.configure(yscrollcommand = ascrollbar.set)
ascrollbar.grid(row=1, column=0)

CodePudding user response:

The problem is that both the scrollbar and listbox are in the same column. If you put the scrollbar in some other column it will show up.

However, you also have the problem that a) the listbox is more than twice the height of the window, and b) the scrollbar is not being stretched vertically to fill the column. So, the scrollbar is centered vertically in the column and thus is off the edge of the window.

If you add sticky='ns' when calling grid on the scrollbar, it will show up. It would also show up if you made the listbox much smaller and with less padding.

  • Related