1
votes

I have a tkinter listbox inside a frame which is inside a canvas. The scrollbar works very well through this code:

messagesList.config(yscrollcommand = scrollbar.set)
        scrollbar.config(command=messagesList.yview)

However, I wanted the scrollbar to be longer so that I can actually scroll. I tried things such as making the frame bigger or make it take more space with padx and pady. Is there any way to make the scrollbar longer?

The image of how it currently is:

enter image description here

Complete code:

import tkinter as tk
from tkinter import ttk
import tkinter.font as tkFont

root = tk.Tk()
root.title("Messager")
root.geometry("1300x700")
root.resizable(height = 0, width = 0)
#makes window not resizable

class Elements:

    def __init__(self, main):

        def sendMessage():
            user = userName.get()
            message = userMessage.get()
            print(user+ " username")
            print(message + " message")

        theCan = tk.Canvas(main)
        titleFrame = tk.LabelFrame(main)
        mainFrame = tk.LabelFrame(theCan)
        greeting = tk.Label(titleFrame, text="Messager", bg = "#74a5f2", font = (None, 35))
        userName = tk.Entry(titleFrame, font = (None, 15))
        userName.insert(0, "An Unnamed Scrub")
        userMessage = tk.Entry(titleFrame, font = (None, 15))
        userMessage.insert(0,"Your Message")
        sendButton = tk.Button(titleFrame, command = sendMessage, text = "Send!", bg = "#74a5f2", font = (None, 22))
        titleMessage = tk.Label(mainFrame, text = "MESSAGER", bg = "#74a5f2", font = (None, 50))
        messagesList = tk.Listbox(mainFrame)
        scrollbar = tk.Scrollbar(mainFrame, orient=tk.VERTICAL, relief = 'flat')

        messagesList.config(yscrollcommand = scrollbar.set)
        scrollbar.config(command=messagesList.yview)


        testList = ["apple", "orange","apple", "orange","apple", "orange","apple", "orange","apple", "orange","apple", "orange","apple", "orange"]

        for item in testList:
            messagesList.insert(tk.END, item)

        placeholder = tk.Label(main, text = "    ")
        placeholder1 = tk.Label(main, text = "    ")
        placeholder2 = tk.Label(main, text = "    ")
        placeholder3 = tk.Label(main, text = "    ")
        placeholder4 = tk.Label(main, text = "    ")
        placeholder5 = tk.Label(main, text = "    ")
        placeholder6 = tk.Label(main, text = "    ")
        placeholder7 = tk.Label(main, text = "    ")
        placeholder8 = tk.Label(main, text = "    ")
        placeholder9 = tk.Label(main, text = "    ")
        placeholder10 = tk.Label(main, text = "    ")
        placeholder11 = tk.Label(main, text = "    ")
        placeholder12 = tk.Label(main, text = "    ")
        placeholder13 = tk.Label(main, text = "    ")
         

        placeholder.grid(row = 1, column = 1)
        placeholder1.grid(row = 2, column = 2)
        placeholder2.grid(row = 3, column = 3)
        placeholder3.grid(row = 4, column = 4)
        placeholder4.grid(row = 5, column = 5)
        placeholder5.grid(row = 6, column = 6)
        placeholder6.grid(row = 7, column = 7)
        placeholder7.grid(row = 8, column = 8)
        placeholder8.grid(row = 1, column = 9)
        placeholder9.grid(row = 1, column = 10)
        placeholder10.grid(row = 1, column = 11)
        placeholder11.grid(row = 1, column = 12)
        placeholder12.grid(row = 1, column = 13)
        placeholder13.grid(row = 1, column = 14)

        #placeholders to move the mainframe frame to the center


        titleFrame.grid(row = 1, padx = 20, pady = 20)

        greeting.grid(pady = 20)
        userName.grid()
        userMessage.grid(pady = 20)
        sendButton.grid()

        mainFrame.grid()
        titleMessage.grid(pady = 20)
        messagesList.grid()
        theCan.grid(row = 1, column = 15, pady = 20)

        scrollbar.grid(sticky = "ne", rowspan = 5)




postEverything = Elements(root)
root.mainloop()
1
You haven't shown us any code that's even slightly relevant to the sizing of the scrollbar. Looks like you used .grid() to position it, but gave it an inappropriate row number. - jasonharper
Sorry I didn't add the entire code block. I used sticky and rowspan to try to make the scrollbar a bit wider. - skumar
You just need to be deligent about putting it in the correct row and having it span the correct number of rows. You've told it to span its own row plus the next four rows below it. It helps to always explicitly set the row and column numbers when calling grid. - Bryan Oakley

1 Answers

0
votes

As has been said already, you need to place the box and the scroll bar on the same row using grid

    messagesList = tk.Listbox(mainFrame)
    scrollbar = tk.Scrollbar(mainFrame, orient=tk.VERTICAL, relief = 'flat')

    messagesList.config(yscrollcommand = scrollbar.set)
    scrollbar.config(command=messagesList.yview)

    messagesList.grid(row=0,column=0)
    scrollbar.grid(row=0,column=1,sticky = "ns")

Also note that I've changed the sticky attribute to be "ns" which means the scroll bar will stretch to fit the row and match the height of the messagesList widget.