[英]python tkinter scrollable frame
I have made a scrollable frame in tkinter
but there are a few things that aren't working. 我在tkinter
制作了一个可滚动的框架,但是有些事情不起作用。
-Is there a way to bring the scrollbar
closer to the labels
? -是否可以将scrollbar
拉近labels
?
-Is there a way to start the scrollbar
on top and not in the middle? -是否有办法在顶部而不是在中间启动scrollbar
?
-When I use bind
I cannot scroll when my mouse is over the labels
, and when I use bind_all
I can scroll everywhere on the window. -当我使用bind
时,当鼠标悬停在labels
上方时,我无法滚动;当我使用bind_all
我可以在窗口中的任何位置滚动。 Is there a way to scroll only when my mouse is above the label
? 只有当鼠标在label
上方时,才可以滚动吗?
My code: 我的代码:
from tkinter import *
class Example:
def __init__(self, root):
self.master = root
self.init_scrollframe()
def init_scrollframe(self):
self.big_frame = Frame(self.master)
self.big_frame.grid(row=0, column=0)
self.title = Label(self.big_frame, text="TITLE", font=("Arial", 30))
self.title.grid(row=0, column=0, columnspan=2)
self.frame = Frame(self.big_frame)
self.frame.grid(row=1, column=0)
self.scrollb = Scrollbar(self.frame, orient=VERTICAL)
self.scrollb.grid(row=0, column=1, sticky=N+S+W)
self.scroll_canvas = Canvas(self.frame, yscrollcommand=self.scrollb.set)
self.scroll_canvas.grid(row=0, column=0, sticky=N+S+E)
self.scrollb.config(command=self.scroll_canvas.yview)
##this doesn't work
"""self.frame.bind("<MouseWheel>", self._on_mousewheel)
self.frame.bind("<Button-5>", self._on_mousewheel)
self.frame.bind("<Button-4>", self._on_mousewheel)"""
##this works but it is scrolling also when I am not above the labels
self.scroll_canvas.bind_all("<MouseWheel>", self._on_mousewheel)
self.scroll_canvas.bind_all("<Button-5>", self._on_mousewheel)
self.scroll_canvas.bind_all("<Button-4>", self._on_mousewheel)
self.my_frame = Frame(self.scroll_canvas)
##put some labels into the scrollable frame
for i in range(100):
self.lbl = Label(self.my_frame, text=str("label number " + str(i)))
self.lbl.grid(row=i, column=0)
self.scroll_canvas.create_window(0, 0, window=self.my_frame)
self.my_frame.update_idletasks()
self.scroll_canvas.config(scrollregion=self.scroll_canvas.bbox("all"))
##this is for scrolling
def _on_mousewheel(self, event):
if event.num == 5 or event.delta == -120:
self.scroll_canvas.yview_scroll(1, "units")
if event.num == 4 or event.delta == 120:
self.scroll_canvas.yview_scroll(-1, "units")
if __name__ == "__main__":
root = Tk()
ex = Example(root)
root.mainloop()
For your 3rd question, you can simply do a check on whether the event is within the frame before your scroll_canvas
. 对于第三个问题,您可以简单地检查事件是否在scroll_canvas
之前的scroll_canvas
。
from tkinter import *
class Example:
def __init__(self, root):
self.master = root
self.init_scrollframe()
def init_scrollframe(self):
self.big_frame = Frame(self.master)
self.big_frame.grid(row=0, column=0)
self.title = Label(self.big_frame, text="TITLE", font=("Arial", 30))
self.title.grid(row=0, column=0, columnspan=2)
self.frame = Frame(self.big_frame,name="test") #give it a name to check
self.frame.grid(row=1, column=0)
self.scrollb = Scrollbar(self.frame, orient=VERTICAL)
self.scrollb.grid(row=0, column=1, sticky=N+S+W)
self.scroll_canvas = Canvas(self.frame, yscrollcommand=self.scrollb.set)
self.scroll_canvas.grid(row=0, column=0, sticky=N+S+E)
self.scrollb.config(command=self.scroll_canvas.yview)
self.scroll_canvas.bind_all("<MouseWheel>", self._on_mousewheel)
self.scroll_canvas.bind_all("<Button-5>", self._on_mousewheel)
self.scroll_canvas.bind_all("<Button-4>", self._on_mousewheel)
self.my_frame = Frame(self.scroll_canvas)
for i in range(100):
self.lbl = Label(self.my_frame, text=str("label number " + str(i)))
self.lbl.grid(row=i, column=0)
self.scroll_canvas.create_window(0, 0, window=self.my_frame)
self.my_frame.update_idletasks()
self.scroll_canvas.config(scrollregion=self.scroll_canvas.bbox("all"))
def _on_mousewheel(self, event):
caller = event.widget
if "test" in str(caller): #check if event is within frame
if event.num == 5 or event.delta == -120:
self.scroll_canvas.yview_scroll(1, "units")
if event.num == 4 or event.delta == 120:
self.scroll_canvas.yview_scroll(-1, "units")
if __name__ == "__main__":
root = Tk()
ex = Example(root)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.