0

My code is working well in itself, but doesn't scroll through the Labels (which is what i'm trying to achieve). I don't want to use canvas or listbox or anything.

import tkinter as tk
master = tk.Tk()

scrollbar = tk.Scrollbar(master).pack(side=tk.RIGHT, fill=tk.Y,command=tk.yview)

label = tk.Label(text="llklkl")
label.place(x=100,y=500)

label2 = tk.Label(text="llklkl")
label2.place(x=1000,y=5000)

tk.mainloop()
Flob
  • 898
  • 1
  • 5
  • 14
Naji El
  • 3
  • 1
  • 2
  • If you want to scroll through multiple widgets of any kind you will be required to use a canvas to accomplish this. There is no way around it. Take a look at [this post](https://stackoverflow.com/a/3092341/7475225) for details. – Mike - SMT Jan 11 '19 at 15:14

1 Answers1

1

Hello and welcome to SO. The tkinter Scrollbar widget sadly can not be used on a _tkinter.tkapp object, i.e. your main window called master. From effbot.org:

This widget is used to implement scrolled listboxes, canvases, and text fields.

and

The Scrollbar widget is almost always used in conjunction with a Listbox, Canvas, or Text widget. Horizontal scrollbars can also be used with the Entry widget.

That means that you absolutely HAVE to create some widget inside your main window in order to be able to scroll anything, you can`t just scroll the window itself. That being said, if you wanted to add a Scrollbar to, let's say, a Listbox, that's how you would do it (also taken from the above mentioned website, you should really check it out):

First of all, you have to set the widget’s yscrollcommand callbacks to the set method of the scrollbar. Secondly, you have to set the scrollbar’s command to the yview method of the widget, much like you did already, but like name_of_object.yview, not tk.yview.

import tkinter as tk

master = tk.Tk()

scrollbar = tk.Scrollbar(master)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

listbox = tk.Listbox(master, yscrollcommand=scrollbar.set)
for i in range(20):
    listbox.insert(tk.END, str(i))
listbox.pack(side=tk.LEFT, fill=tk.BOTH)

scrollbar.config(command=listbox.yview)

master.mainloop()

Also, pack the scrollbar in a seperate line. This will produce a window with numbers from 1 to 50 in a scrollable Listbox widget. If i get you right, you want to be able to scroll between your labels? well, i guess you'll have to use some kind of wrapping widget for that, i would recommend a Canvas. But that's really up to you and i'm sure you'll figure it out yourself. If you need any more help, let me know - but please read the docs before asking ;-)

Flob
  • 898
  • 1
  • 5
  • 14