'Why scrollbar won't show on listbox?

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?



Solution 1:[1]

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)

Solution 2:[2]

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.

Solution 3:[3]

I had a similar experience to yours. Actually, I used irc chat to parse names. But you can see my example. You can either leave a comment or not. Btw, I do not use resizing. You can also change it.

#!/usr/bin/python3.11.0b1
import tkinter as tk

userNames = 'wendy_25 andywolf StePHen_0 Phoneboy suspect0 @xpc` picturesque kazzie_f_ nj_pinoy African_m pSyLock3` a_john FiftyShadesOfJhay dadENGR bords b3bang ^lucia^ Frailty ^Bagito^ Turkish_Male_With_Cam missy`mia [Cher] Blacklsted Ramses-II @gracie Chuck_42 Ahkong- asl_pls Allah[swt] Sorbetero @TrooperBuggs Alain- Single_Mom45 Demonya Tomaz tropix tapsilog mib_mnbvcxz @Bulate Guest49512 mib_xcvb S1l-v3r +BURNgirl TrEz`U'
#userNames =""
class App(tk.Frame):
    def __init__(self, master):
        """Create our widgets"""
        tk.Frame.__init__(self,master)
        self.userNameList = tk.Listbox(self, width = 25)
        self.scrUserNameList = tk.Scrollbar(self, command=self.userNameList.yview, orient=tk.VERTICAL)
        self.userNameList.config(yscrollcommand=self.scrUserNameList.set)
        self.scrUserNameList.pack(side=tk.RIGHT, fill=tk.BOTH)
        self.userNameList.pack(side=tk.RIGHT, fill=tk.BOTH)

    def populateList(self,data):
        """Adds items to the list from a string of space seperated items"""
        users = data.strip().split()
        for user in users:
            self.userNameList.insert(tk.END,user)

if __name__ == '__main__':
    root = tk.Tk()
    #Create the Application
    app = App(root)
    app.pack()
    #Before we start the mainloop, populate the list.
    app.populateList(userNames)
    root.mainloop()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Bryan Oakley
Solution 3 toyota Supra