[Python] Cannot make tkinter horizontal scrollbar lie flush against listbox

Discussão em 'Python' iniciado por Stack, Agosto 29, 2025.

  1. Stack

    Stack Membro Participativo

    In a tkinter window, I have several widgets including a listbox and a horizontal scrollbar (image below).

    [​IMG]

    Try as I might, I cannot make the scrollbar lie flush against the listbox. Based on Google's suggestions, I have tweaked settings for highlightthickness, padding and sticky, to no avail.

    For the purpose of providing minimal reproducible code, I have recreated my issue using a window containing the image on the left and a listbox and a horizontal scrollbar on the right.

    import tkinter as tk

    window = tk.Tk()
    listbox = tk.Listbox(window)
    x_scrollbar = tk.Scrollbar(window)

    canvas = tk.Canvas(window, width=205, height=205)
    logo = tk.PhotoImage(file="password_img.png")
    canvas.logo = logo
    canvas.create_image(102, 102, image=logo)
    canvas.config(highlightthickness=0)
    canvas.grid(column=0, row=0, rowspan=2)

    listbox.config(height=4, xscrollcommand=x_scrollbar.set)
    listbox.grid(column=1, row=0, sticky="EW")

    x_scrollbar.config(orient="horizontal", command=listbox.xview)
    x_scrollbar.grid(column=1, row=1, sticky="EW")

    window.mainloop()


    BTW, I have noticed that if I comment out the canvas, then all the widgets lie flush against each other. However, I'm not sure what to do with this information. Besides, strictly speaking, I want the vertical gaps between the other widgets to remain and only the vertical gap between scrollbar and listbox to disappear.

    Will using a frame to hold the listbox and scrollbar solve my problem? I haven't tried this out because I don't yet know how to use frames.

    Continue reading...

Compartilhe esta Página