问题描述
我正在尝试制作带有标签的应用。我的选项卡上会有很多按钮,因此每个选项卡上都需要一个可滚动的框架。以下是我从here获取的代码:
import tkinter as tk
from tkinter import ttk
# ************************
# Scrollable Frame Class
# ************************
class ScrollFrame(tk.Frame):
def __init__(self,parent):
super().__init__(parent) # create a frame (self)
self.canvas = tk.Canvas(self,borderwidth=0,background="#ffffff") #place canvas on self
self.viewPort = tk.Frame(self.canvas,background="#ffffff") #place a frame on the canvas,this frame will hold the child widgets
self.vsb = tk.Scrollbar(self,orient="vertical",command=self.canvas.yview) #place a scrollbar on self
self.canvas.configure(yscrollcommand=self.vsb.set) #attach scrollbar action to scroll of canvas
self.vsb.pack(side="right",fill="y") #pack scrollbar to right of self
self.canvas.pack(side="left",fill="both",expand=True) #pack canvas to left of self and expand to fil
self.canvas_window = self.canvas.create_window((4,4),window=self.viewPort,anchor="nw",#add view port frame to canvas
tags="self.viewPort")
self.viewPort.bind("<Configure>",self.onFrameConfigure) #bind an event whenever the size of the viewPort frame changes.
self.canvas.bind("<Configure>",self.onCanvasConfigure) #bind an event whenever the size of the viewPort frame changes.
self.canvas.bind_all("<MouseWheel>",self.onScroll) #bind an event whenever the size of the viewPort frame changes.
self.onFrameConfigure(None) #perform an initial stretch on render,otherwise the scroll region has a tiny border until the first resize
def onFrameConfigure(self,event):
'''Reset the scroll region to encompass the inner frame'''
self.canvas.configure(scrollregion=self.canvas.bbox("all")) #whenever the size of the frame changes,alter the scroll region respectively.
def onCanvasConfigure(self,event):
'''Reset the canvas window to encompass inner frame when required'''
canvas_width = event.width
self.canvas.itemconfig(self.canvas_window,width = canvas_width) #whenever the size of the canvas changes alter the window region respectively.
def onScroll(self,event):
if event.state == 0:
self.canvas.yview_scroll(int(-1*(event.delta/120)),"units")
return "break"
elif event.state == 1:
self.canvas.xview_scroll(int(-1*(event.delta/120)),"units")
return "break"
# ********************************
# Example usage of the above class
# ********************************
class Example(tk.Frame):
def __init__(self,root):
tk.Frame.__init__(self,root)
self.scrollFrame = ScrollFrame(self) # add a new scrollable frame.
# Now add some controls to the scrollframe.
# NOTE: the child controls are added to the view port (scrollFrame.viewPort,NOT scrollframe itself)
for row in range(100):
a = row
tk.Label(self.scrollFrame.viewPort,text="%s" % row,width=3,borderwidth="1",relief="solid").grid(row=row,column=0)
t="this is the second column for row %s" %row
tk.Button(self.scrollFrame.viewPort,text=t,command=lambda x=a: self.printMsg("Hello " + str(x))).grid(row=row,column=1)
# when packing the scrollframe,we pack scrollFrame itself (NOT the viewPort)
self.scrollFrame.pack(side="top",expand=True)
def printMsg(self,msg):
print(msg)
if __name__ == "__main__":
root=tk.Tk()
main_frame = tk.Frame(root)
nb = ttk.Notebook(main_frame)
frame = Example(nb)
#frame = tk.Frame(main_frame)
frame.pack()
nb.add(frame,text = "frame1")
frame1 = Example(nb)
frame1.pack()
nb.add(frame1,text = "frame2")
nb.pack(side = "left")
main_frame.pack()
root.mainloop()
问题是我只能使用鼠标滚轮滚动笔记本中最后添加的标签页。可滚动框架按预期工作于单个选项卡。但是,当添加多个标签时,我只能通过鼠标滚轮滚动最后添加的标签。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)