我已经四处搜索,似乎无法找到问题的答案。我正在尝试为以下代码创建一个工作滚动条,似乎无法让它工作。问题似乎与OnFrameConfigure方法有关。我在其他地方看到该方法应该是def OnFrameConfigure(event):
但是当我将(event)
部分放入我的方法时,除非我在class
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
'''This initialisation runs the whole program'''
#tk.Tk.__init__(self, *args, **kwargs)
main = tk.Tk()
canvas = tk.Canvas(main)
scroll = tk.Scrollbar(main, orient='vertical', command=canvas.yview)
canvas.configure(yscrollcommand=scroll.set)
frame = tk.Frame(canvas)
scroll.pack(side='right', fill='y')
canvas.pack(side='left', fill='both', expand='yes')
canvas.create_window((0,0), window=frame)
frame.bind('<Configure>', self.OnFrameConfigure(parent=canvas))
for i in range(100):
tk.Label(frame, text='I am a Label').pack()
main.mainloop()
def OnFrameConfigure(self, parent):
'''Used to allowed scrolled region in a canvas'''
parent.configure(scrollregion=parent.bbox('all'))
答案 0 :(得分:2)
Your problem starts here:
frame.bind('<Configure>', self.OnFrameConfigure(parent=canvas))
You are immediately calling the OnFrameConfigure
function. That is not how you use bind
. You must give a reference to a callable function. Since you're using a class, you don't need to pass parent
in, unless you have this one function work for multiple widgets.
Change the binding to this:
frame.bind('<Configure>', self.OnFrameConfigure)
Change the method definition to this:
def OnFrameConfigure(self, event):
Finally, your __init__
needs to save a reference to the canvas, which you can then use in that function:
def __init__(self, *args, **kwargs):
...
self.canvas = tk.Canvas(...)
...
...
def OnFrameConfigure(self, event):
...
self.canvas.configure(scrollregion=self.canvas.bbox('all'))
...