0

I'm using tkinter, this is my code:

import tkinter as tk

root = tk.Tk()

frame1 = tk.Frame(root, bg=root['background'])
frame1.pack(expand='YES')

canvas = tk.Canvas(frame1, width=500, height=100, bg="#B8D8D8", relief="raised")
canvas.configure(scrollregion=canvas.bbox("ALL"))
canvas.pack(expand='YES', fill='both')
scroll_x = tk.Scrollbar(frame1, orient="horizontal")
scroll_x.config(command=canvas.xview)
scroll_x.pack(side="top", fill='x')
canvas.configure(xscrollcommand=scroll_x.set)

start = int(50)
file_names = [....] # a list of many strings
for name in file_names:
    frame = tk.Frame(canvas, bg=canvas['background']) # the frame is necessary since in the real code i'm using an image widget as well
    lbl = tk.Label(frame, text=name, bg=canvas['background'])
    bl.pack()
    frame.pack()
    canvas.create_window(start, 50, window=frame)
    start = start + 100

The canvas won't scroll and many strings of which I'd like to display get lost. The root is non-resizable.

Maya
  • 25
  • 4
  • 1
    You should call `canvas.configure(scrollregion=canvas.bbox("all"))` whenever you change the contents of the canvas. To make it easier just use `canvas.bind("", lambda e: canvas.configure(scrollregion=canvas.bbox("ALL")))` – TheLizzard May 19 '21 at 14:07
  • 1
    Also you shouldn't call `frame.pack()`. `canvas.create_window(...)` should do that for you – TheLizzard May 19 '21 at 14:08

0 Answers0