网上搜来的用例
from tkinter import *
def data():
for i in range(50):
Label(frame,text=i).grid(row=i,column=0)
Label(frame,text="my text"+str(i)).grid(row=i,column=1)
Label(frame,text="..........").grid(row=i,column=2)
# 少了这个就滚动不了
def myfunction(event):
canvas.configure(scrollregion=canvas.bbox("all"),width=200,height=200)
root=Tk()
sizex = 800
sizey = 600
posx = 100
posy = 100
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
myframe=Frame(root,relief=GROOVE,width=50,height=100,bd=1)
myframe.place(x=10,y=10)
canvas=Canvas(myframe)
frame=Frame(canvas)
myscrollbar=Scrollbar(myframe,orient="vertical",command=canvas.yview)
canvas.configure(yscrollcommand=myscrollbar.set)
myscrollbar.pack(side="right",fill="y")
canvas.pack(side="left")
canvas.create_window((0,0),window=frame,anchor='nw')
frame.bind("<Configure>",myfunction)
data()
root.mainloop()
实际演练
def refresh_scroll(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"),width=600,height=600)
def __init_gui(self):
self.__lb_frame = tk.Frame(self)
self.__init_mod_listbox(self.__lb_frame)
self.__lb_frame.pack(side=tk.LEFT, expand=0)
_f = tk.Frame(self, width=600, height=600)
_f.pack(side=tk.LEFT, expand=0)
_f.pack_propagate(0)
canvas=tk.Canvas(_f, width=600,height=600)
__options_f = tk.Frame(canvas)
__options_f.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
__options_f.bind("<Configure>",self.refresh_scroll)
hbar=tk.Scrollbar(_f, orient=tk.HORIZONTAL)
hbar.pack(side=tk.BOTTOM, fill=tk.X, expand=0)
hbar.config(command=canvas.xview)
vbar=tk.Scrollbar(_f, orient=tk.VERTICAL)
vbar.pack(side=tk.RIGHT, fill=tk.Y)
vbar.config(command=canvas.yview)
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
canvas.pack(side=tk.LEFT, expand=False, fill=tk.BOTH)
canvas.create_window((400,1600),window=__options_f,anchor='nw')
self.canvas = canvas
少了这行无效:
def refresh_scroll(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"),width=600,height=600)