我正在创建一个按钮滑块,但是如果我对按钮使用.pack
方法,它不会滑动按钮。
# 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()
我该如何欺骗这个?预先感谢。
答案 0 :(得分:0)
如果你想在 tkinter 中制作滑动按钮,那么使用这个我希望你喜欢这个:-
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()
答案 1 :(得分:0)
如果你想在 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)