2

I have written this code in python tkinter but i want to add a vertical & a horizontal scrollbar so that i can scroll my buttons vertically & horizontally. I just want a simple so that as a beginner i can understand it thoroughly. Any help is appreciated.

from tkinter import *

root = Tk()

root.geometry("555x350")

b = Button(root, text="Sample button 1")
b.place(x=0, y=0)

b = Button(root, text="Sample button 1")
b.place(x=0, y=30)

b = Button(root, text="Sample button 1")
b.place(x=0, y=90)


b = Button(root, text="Sample button 1")
b.place(x=0, y=160)


b = Button(root, text="Sample button 1")
b.place(x=0, y=334)

b = Button(root, text="Sample button 1")
b.place(x=120, y=0)


b = Button(root, text="Sample button 1")
b.place(x=230, y=0)


b = Button(root, text="Sample button 1")
b.place(x=472, y=0)

root.mainloop()
  • 1
    Put the buttons in a frame like [this](https://stackoverflow.com/a/66215091/11106801) one. Also I suggest using `.pack` or `.grid` instead of `.place` – TheLizzard Aug 22 '21 at 15:45
  • I tried doing that but i can't make a frame scrollable. – Jashodhan Aug 22 '21 at 15:47
  • Why didn't it work? There are 2 ways of making something scrollable in `tkinter`, and using a `Frame` inside a `Canvas` (like the link I posted), is the easier way (in my opinion). – TheLizzard Aug 22 '21 at 15:49
  • Ok I'll look forward to it & later will tell if it works. – Jashodhan Aug 22 '21 at 15:53
  • Dear TheLizzard can you give me a simple code because I'm a beginner in tkinter & its hard for me to understand your code. – Jashodhan Aug 22 '21 at 15:57
  • The code there comes with 2 examples (run the code and they will automatically start). Just change the `tk.Label`s to `tk.Button`s, if you want buttons instead of labels. Also do you know how for loops work? – TheLizzard Aug 22 '21 at 15:59
  • I am not used to go with loops for making buttons in tkinter. – Jashodhan Aug 22 '21 at 16:04
  • 1
    Please look at some basic python tutorials and make sure you don't fall in [this](https://stackoverflow.com/q/10865116/11106801) trap. Also please note that making multiple widget scrollable (like what you need), isn't part of the basics. – TheLizzard Aug 22 '21 at 16:06

1 Answers1

3

tkinter doesn't support scrolling widgets inside a frame. The only vertically scrollable widgets are the Text, Canvas, Listbox, and ttk.Treeview widgets.

Since you are using place for the buttons, you can instead add the buttons to a canvas since you can position widgets at exact coordinates. I've shown an example at the end of this pot.

If you prefer to use grid or pack to arrange, see Adding a scrollbar to a group of widgets in Tkinter which shows how to put your buttons in a frame, put the frame in a canvas, and then scroll the frame as part of the canvas.

Here's how to do it if you want to place the widgets at specific coordinates by adding them directly to a canvas.

from tkinter import *

root = Tk()

root.geometry("555x350")

canvas = Canvas(root)
vsb = Scrollbar(root, orient="vertical", command=canvas.yview)
hsb = Scrollbar(root, orient="horizontal", command=canvas.xview)

root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
canvas.configure(xscrollcommand=hsb.set, yscrollcommand=vsb.set)
canvas.grid(row=0, column=0, sticky="nsew")
vsb.grid(row=0, column=1, sticky="ns")
hsb.grid(row=1, column=0, sticky="ew")

b1 = Button(canvas, text="Sample button 1")
b2 = Button(canvas, text="Sample button 1")
b3 = Button(canvas, text="Sample button 1")
b4 = Button(canvas, text="Sample button 1")
b5 = Button(canvas, text="Sample button 1")
b6 = Button(canvas, text="Sample button 1")
b7 = Button(canvas, text="Sample button 1")
b8 = Button(canvas, text="Sample button 1")

canvas.create_window(0, 0, anchor="nw", window=b1)
canvas.create_window(0, 30, anchor="nw", window=b2)
canvas.create_window(0, 90, anchor="nw", window=b3)
canvas.create_window(0, 160, anchor="nw", window=b4)
canvas.create_window(0, 334, anchor="nw", window=b5)
canvas.create_window(120, 0, anchor="nw", window=b6)
canvas.create_window(230, 0, anchor="nw", window=b7)
canvas.create_window(472, 0, anchor="nw", window=b8)

canvas.bind("<Configure>", lambda event: canvas.configure(scrollregion=canvas.bbox("all")))

root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks for help Bryan Oakly but i need a simpler code because as a beginner i can't understand some specific things. – Jashodhan Aug 23 '21 at 12:40