I am writing a small program, which the intention to run a resize method, when the window size is changed. for this reason, I used toplevel.bind("<Configure>", self.resize)
. While this does work for resizing, the method gets called hundreds of times: when scrolling, when using some of the buttons, even though the window size doesn't change with any of these actions. How can I bind the event, so that it only gets called upon changing the window size?
import tkinter as tk
def resize(event):
print("widget", event.widget)
print("height", event.height, "width", event.width)
parent = tk.Toplevel()
canvas = tk.Canvas(parent)
scroll_y = tk.Scrollbar(parent, orient="vertical", command=canvas.yview)
scroll_x = tk.Scrollbar(parent, orient="horizontal", command=canvas.xview)
frame = tk.Frame(canvas)
# put the frame in the canvas
canvas.create_window(0, 0, anchor='nw', window=frame)
# make sure everything is displayed before configuring the scrollregion
canvas.update_idletasks()
canvas.configure(scrollregion=canvas.bbox('all'),
yscrollcommand=scroll_y.set,
xscrollcommand=scroll_x.set)
scroll_y.pack(fill='y', side='right')
scroll_x.pack(fill='x', side='bottom')
canvas.pack(fill='both', expand=True, side='left')
parent.bind("<Configure>", resize)
parent.mainloop()
Output:
widget .!toplevel
height 200 width 200
widget .!toplevel.!scrollbar
height 286 width 17
widget .!toplevel.!scrollbar2
height 17 width 382
widget .!toplevel.!canvas
height 269 width 382
widget .!toplevel
height 286 width 399
widget .!toplevel
height 286 width 399
widget .!toplevel.!can
Eventhough there is only one canvas plus the scrollbars on the window, the resize event gets called 7 times, before the user even has a change to interact with the window. How can I stop this madness and only call the resize function, when the window does get resized?