0

I am creating a button slider, but it doesn't slides buttons if I use .pack method for buttons.

# imports

if __name__ == "__main__":
    root = tk.Tk()
    root.title("Procket")
    root.geometry("500x500")

    listbox = tk.Listbox(root)

    canvas = tk.Canvas(root, bg="white")
    scrollbar = tk.Scrollbar(root, orient=tk.VERTICAL, command=listbox.yview)
    scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
    canvas.config(yscrollcommand=scrollbar.set)

    filename = filedialog.askopenfilename(
        initialdir="C:",
        title="Select file"
    )

    path = "my/path"
    for file in os.listdir(path):
        image = Image.open(path+file)
        photo = ImageTk.PhotoImage(image)
        button = tk.Button(root, text="фыв", image=photo)
        button.pack()

        listbox.insert(tk.END, button)

    listbox.pack()
    scrollbar.config(command=listbox.yview)

    root.mainloop()

How can I fox this? Thanks in advance.

  • 1
    see: [listbox is only for strings](https://stackoverflow.com/a/5614607/1832058) – furas Apr 08 '19 at 21:08
  • there are examples how to use `Canvas` with `Frame` to create [Scrolled Frame](https://stackoverflow.com/questions/16188420/tkinter-scrollbar-for-frame) and you can put any widget(s) in Frame. – furas Apr 08 '19 at 21:10
  • What is a "button slider"? tkinter has a slider widget, but you're apparently using a listbox. Are you asking how to create a scrollable group of buttons? – Bryan Oakley Apr 08 '19 at 21:46

2 Answers2

0

if you want to make slide button in tkinter so use this I hope you like this:-


import tkinter
from tkinter import *


def Slide_button(master, bg, slide_paddle_bg, sliding_ball_bg, x, y):
    slide_canvas = Canvas(master, width=80, height=40, bg=bg)

    indexer = 1

    def call_choose(event):  # this is main function for control
        nonlocal indexer
        global state
        if indexer == 1:
            on_slide()
            indexer += 1
            state_ret("on")
        elif indexer == 2:
            on_slide2()
            indexer -= 1
            state_ret("off")

    i = 20

    def on_slide():  # this function for animation right to left
        nonlocal i
        slide_button.place(x=i, y=13)
        i += 3
        if i < 50:
            master.after(1, on_slide)

    def on_slide2():  # this function for left to right
        nonlocal i
        slide_button.place(x=i, y=12)
        i -= 3
        if i > 17:
            master.after(1, on_slide2)

    def caller_for_slide_button():
        call_choose(0)

    slide_frame = Frame(slide_canvas, width=50, height=18, bg=slide_paddle_bg)
    slide_button = Button(slide_canvas, width=2, bg=sliding_ball_bg, command=caller_for_slide_button)
    slide_frame.bind("<Button-1>", call_choose)
    slide_button.place(x=20, y=12)
    slide_frame.place(x=20, y=16)
    slide_canvas.place(x=x, y=y)


root = tkinter.Tk()

Slide_button(root, "red", "cyan", "blue", 30, 30)


def state_ret(state):
    print(state)


root.mainloop()
return007
  • 823
  • 1
  • 7
  • 19
umesh
  • 1
  • 1
    Mind explaining the problem OP faced or how your solution works? I really appreciate that you provided the complete solution but some pointers on what you did different from OP will be even more helpful. All the best with your future contributions in SO! – return007 May 16 '21 at 09:09
0

Try this if you want to make a slide button in tkinter:

import tkinter
from tkinter import *

def Slide_button(master, bg, slide_paddle_bg, sliding_ball_bg, x, y, command):
    """
    :param master: give canvas frame root where you want to made slide button.
    :param bg: give background of slide button.
    :param slide_paddle_bg: give background of slide paddle in which button is slide.
    :param sliding_ball_bg: give background of button that is slide on paddle.
    :param x: give position x of slide button.
    :param y: give position x of slide button.
    :param command: give a function take state argument.
    :return: it not return any thing
    
    msg= the given function in command it is call by this function.
    """

    slide_canvas = Frame(master, width=80, height=40, bg=bg)

    indexer = 1
    def call_choose(event):  # this is main function for control
        nonlocal indexer
        if indexer == 1:
            on_slide()
            indexer += 1
            command(state="on")
        elif indexer == 2:
            on_slide2()
            indexer -= 1
            command(state="off")

    i = 20

    def on_slide():  # this function for animation right to left
        nonlocal i
        slide_button.place(x=i, y=13)
        i += 3
        if i < 50:
            master.after(1, on_slide)

    def on_slide2():  # this function for left to right
        nonlocal i
        slide_button.place(x=i, y=12)
        i -= 3
        if i > 17:
            master.after(1, on_slide2)

    def caller_for_slide_button():
        call_choose(0)

    slide_frame = Frame(slide_canvas, width=50, height=18, bg=slide_paddle_bg)
    slide_button = Button(slide_canvas, width=2, bg=sliding_ball_bg, command=caller_for_slide_button)
    slide_frame.bind("<Button-1>", call_choose)
    slide_button.place(x=20, y=12)
    slide_frame.place(x=20, y=16)
    slide_canvas.place(x=x, y=y)
jps
  • 20,041
  • 15
  • 75
  • 79
umesh
  • 1
  • Why did you add another answer without any explanation? Is this different from the other one? If you want to improve/correct an existing answer, just edit it. And please explain how you solve the problem instead of just answering with code. – jps May 17 '21 at 09:23
  • @jps I try to do it as u basically I am new in this site so I don't know too much say thx. :) – umesh Aug 24 '21 at 10:34