From 350c50ae49efdfa73393bed214030b4b87adfd4b Mon Sep 17 00:00:00 2001 From: Julian Mehne Date: Sat, 14 Sep 2019 21:21:58 +0200 Subject: [PATCH] Embedding in Tk example: Fix toolbar being clipped. Calling `pack` without options puts the widget at the end of the package list. Space is allocated to widgets sequentially. If a window is too small, there may be no space left for widgets. By packing the canvas last, we essentially give the toolbar and the quit button priority in the packing order. Fixes #13485. --- examples/user_interfaces/embedding_in_tk_sgskip.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/user_interfaces/embedding_in_tk_sgskip.py b/examples/user_interfaces/embedding_in_tk_sgskip.py index 2446619d462e..ad85b7b320d3 100644 --- a/examples/user_interfaces/embedding_in_tk_sgskip.py +++ b/examples/user_interfaces/embedding_in_tk_sgskip.py @@ -25,11 +25,9 @@ canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea. canvas.draw() -canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1) toolbar = NavigationToolbar2Tk(canvas, root) toolbar.update() -canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1) def on_key_press(event): @@ -40,6 +38,12 @@ def on_key_press(event): canvas.mpl_connect("key_press_event", on_key_press) button = tkinter.Button(master=root, text="Quit", command=root.quit) + +# Packing order is important. Widgets are processed sequentially and if there +# is no space left, because the window is too small, they are not displayed. +# The canvas is rather flexible in its size, so we pack it last which makes +# sure the UI controls are displayed as long as possible. button.pack(side=tkinter.BOTTOM) +canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1) tkinter.mainloop()