Home > Software engineering >  Create scrollbar in tKinter without displacing widgets to the left (python)
Create scrollbar in tKinter without displacing widgets to the left (python)

Time:12-08

I'm creating a program by learning from youtube tutorials (I'm a complete beginner) and I have come to some difficulties. This time, I'm trying to create a scrollbar, and I want my widgets to stay on the center of my window, not the left (I'm following the Codemy.com tutorial on scrollbars).

Here is the current aspect of my program: with scrollbar

And here is how I want it to look: without scrollbar

This is my code right now:

import tkinter as tk

root = tk.Tk()
root.geometry("600x400")

my_canvas = tk.Canvas(root)
my_canvas.pack(side = "left", fill = "both", expand = 1)

my_scrollbar = tk.Scrollbar(root, orient = "vertical", command = my_canvas.yview)
my_scrollbar.pack(side = "right", fill = "y")
my_canvas.configure(yscrollcommand = my_scrollbar.set)
my_canvas.bind("<Configure>", lambda e: my_canvas.configure(scrollregion = my_canvas.bbox("all")))


my_frame = tk.Frame(my_canvas)

for i in range(100):
    my_label = tk.Label(my_frame, text = "Label")
    my_label.pack()

my_canvas.create_window((0,0), window = my_frame, anchor = "nw")

root.mainloop()

CodePudding user response:

Include width = 600, anchor = "nw" in my_canvas declaration.

my_canvas.create_window((0,0), window = my_frame, width = 600, anchor = "nw")
  • Related