是否可以使按钮在 canvas 内滚动?
Is it possible to make a button scrollable inside a canvas?
我一直在尝试在 canvas 中制作一个可滚动的按钮,但它不会滚动。
我试过将它放在 ListBox 中,canvas,frame 没用。我已经尝试了一切,但此时仍然不知道该怎么做。
from tkinter import *
FrameU = Tk()
frameN=Frame(FrameU,width=540,height=800,bg="#A0522D")
frameN.place(x=0,y=0,relx=.2,rely=.2)
canvas=Canvas(frameN,bg="#A0522D",width=400,height=800)
scrollbar = Scrollbar(frameN, orient = VERTICAL,width=20,relief=SUNKEN)
scrollbar.pack(side=RIGHT, fill=Y)
scrollbar.config(command=canvas.yview)
canvas.config(width=540,height=800,relief=SUNKEN)
canvas.config(yscrollcommand=scrollbar.set)
mylist=Listbox(canvas,bg="#A0522D",width=100,height=41,bd=5,highlightthickness=0,yscrollcommand=scrollbar.set )
for i in range(100):
mylist.insert(END, i)
b = Button(canvas,bg="blue",width=5,height=5)
b.place(rely=.5,relx=.5)
mylist.config(yscrollcommand=scrollbar.set)
mylist.pack(side=LEFT,fill=BOTH)
canvas.pack(side=LEFT)
我没有收到任何错误或任何错误,但是如果我将它放在我的列表框中,它会显示在 "for i in range(100):" 中 它会显示在 canvas ".!frame.![=19= 中.!listbox.!button
可滚动按钮。你的意思是按钮的位置随着滚动条滑块的移动而变化?在最简单的情况下乍一看似乎是不可能的。
但是你可以给你的滚动条绑定一个回调函数,这个函数会随着鼠标移动滚动条滑块同步改变按钮的位置。这是一个工作示例:
from tkinter import *
from tkinter import ttk
rx, ry = 0.45, 0.45 # the button position (relative)
root=Tk()
wCanvas, hCanvas = 400, 350 # size of canvas
w1, h1 = 800, 700 # size of scrollable area
vBar = ttk.Scrollbar(root, orient = VERTICAL)
can1 = Canvas(root, scrollregion = (0,0,w1,h1), width = wCanvas, height = hCanvas, yscrollcommand = vBar.set)
vBar['command'] = can1.yview
vBar.pack(side=RIGHT,fill=Y)
can1.pack()
can1.create_line(10, 10, 100, 100) # for canvas scrolling confirmation only
but1 = Button(can1)
but1.place(relx = rx, rely = ry)
def VscrollBarMove(event): # callback function
but1.place(relx = rx, rely = ry + vBar.get()[0]) # Scrollbar.get() returns a tuple with relative positions of upper (for vertical scrollbar) and lower edges of a slider
vBar.bind('<B1-Motion>', VscrollBarMove)
root.mainloop()
2 键案例的回调函数版本:
def VscrollBarMove(event): # callback function
but1.place(relx = rx, rely=ry+vBar.get()[0])
but2.place(rely = ry2 + vBar.get()[0]) # You can set the only desirable coordinate if You want
我一直在尝试在 canvas 中制作一个可滚动的按钮,但它不会滚动。
我试过将它放在 ListBox 中,canvas,frame 没用。我已经尝试了一切,但此时仍然不知道该怎么做。
from tkinter import *
FrameU = Tk()
frameN=Frame(FrameU,width=540,height=800,bg="#A0522D")
frameN.place(x=0,y=0,relx=.2,rely=.2)
canvas=Canvas(frameN,bg="#A0522D",width=400,height=800)
scrollbar = Scrollbar(frameN, orient = VERTICAL,width=20,relief=SUNKEN)
scrollbar.pack(side=RIGHT, fill=Y)
scrollbar.config(command=canvas.yview)
canvas.config(width=540,height=800,relief=SUNKEN)
canvas.config(yscrollcommand=scrollbar.set)
mylist=Listbox(canvas,bg="#A0522D",width=100,height=41,bd=5,highlightthickness=0,yscrollcommand=scrollbar.set )
for i in range(100):
mylist.insert(END, i)
b = Button(canvas,bg="blue",width=5,height=5)
b.place(rely=.5,relx=.5)
mylist.config(yscrollcommand=scrollbar.set)
mylist.pack(side=LEFT,fill=BOTH)
canvas.pack(side=LEFT)
我没有收到任何错误或任何错误,但是如果我将它放在我的列表框中,它会显示在 "for i in range(100):" 中 它会显示在 canvas ".!frame.![=19= 中.!listbox.!button
可滚动按钮。你的意思是按钮的位置随着滚动条滑块的移动而变化?在最简单的情况下乍一看似乎是不可能的。
但是你可以给你的滚动条绑定一个回调函数,这个函数会随着鼠标移动滚动条滑块同步改变按钮的位置。这是一个工作示例:
from tkinter import *
from tkinter import ttk
rx, ry = 0.45, 0.45 # the button position (relative)
root=Tk()
wCanvas, hCanvas = 400, 350 # size of canvas
w1, h1 = 800, 700 # size of scrollable area
vBar = ttk.Scrollbar(root, orient = VERTICAL)
can1 = Canvas(root, scrollregion = (0,0,w1,h1), width = wCanvas, height = hCanvas, yscrollcommand = vBar.set)
vBar['command'] = can1.yview
vBar.pack(side=RIGHT,fill=Y)
can1.pack()
can1.create_line(10, 10, 100, 100) # for canvas scrolling confirmation only
but1 = Button(can1)
but1.place(relx = rx, rely = ry)
def VscrollBarMove(event): # callback function
but1.place(relx = rx, rely = ry + vBar.get()[0]) # Scrollbar.get() returns a tuple with relative positions of upper (for vertical scrollbar) and lower edges of a slider
vBar.bind('<B1-Motion>', VscrollBarMove)
root.mainloop()
2 键案例的回调函数版本:
def VscrollBarMove(event): # callback function
but1.place(relx = rx, rely=ry+vBar.get()[0])
but2.place(rely = ry2 + vBar.get()[0]) # You can set the only desirable coordinate if You want