How to move button outside of its parent with tkinter?


While Tkinter excels in creating dynamic applications, certain challenges arise when attempting to manipulate the positions of widgets. In this tutorial, we will explain an advanced Tkinter technique and show how you can move a button beyond its parent's confines.

Understanding Tkinter Hierarchy

Tkinter follows a hierarchical structure where widgets are nested within their parent widgets. A widget's position and visibility are constrained within the boundaries of its parent. This limitation can be a hurdle when attempting to move a widget outside its parent's area. However, by utilizing Toplevel windows, we can create a workaround to overcome this constraint.

Creating a Simple Tkinter Application

Let's begin by creating a basic Tkinter application with a button that will serve as our starting point. We'll then explore how to move this button outside its parent's boundaries.

Example

import tkinter as tk

# Defining the move_button_outside function
def move_button_outside():
   # Create a new Toplevel window
   toplevel = tk.Toplevel(root)
   toplevel.title("Outside Button")

   # Create a button in the Toplevel window
   outside_button = tk.Button(toplevel, text="Outside Button")
   outside_button.pack()

   # Move the Toplevel window to a specific position
   toplevel.geometry("+400+300") 

# Create the main Tkinter window
root = tk.Tk()
root.title("Main Window")
root.geometry("720x250")

# Create a button in the main window
inside_button = tk.Button(root, text="Inside Button")
inside_button.pack()

# Create a button to move the outside button
move_button = tk.Button(root, text="Move Outside Button", command=move_button_outside)
move_button.pack()

# Run the Tkinter event loop
root.mainloop()

In the above example, we did the followings −

  • We import the Tkinter module and alias it as 'tk' for brevity.

  • The move_button_outside function is defined to create a new Toplevel window and place a button inside it.

  • The main Tkinter window (root) contains an "Inside Button" and a "Move Outside Button" that triggers the move_button_outside function.

Output

On running this code, you will get the following output window −

Using Toplevel Windows

The key to moving a widget outside its parent is by employing Toplevel windows. These windows act as independent containers, allowing widgets within them to exist outside the confines of the main window. In our example, clicking the "Move Outside Button" creates a Toplevel window with an "Outside Button."

def move_button_outside():
   # Create a new Toplevel window
   toplevel = tk.Toplevel(root)
   toplevel.title("Outside Button")

   # Create a button in the Toplevel window
   outside_button = tk.Button(toplevel, text="Outside Button")
   outside_button.pack()

   # Move the Toplevel window to a specific position
   toplevel.geometry("+400+100")  # Adjust the coordinates as needed

In the move_button_outside function, a new Toplevel window is created, and a button is placed within it. The geometry method is used to set the position of the Toplevel window, effectively placing it outside the main window. The coordinates specified in the geometry method can be adjusted to achieve the desired position.

Conclusion

Toplevel windows in Tkinter provides a powerful workaround for moving widgets outside their parent boundaries. This advanced technique enables the creation of dynamic and visually compelling user interfaces. By strategically positioning widgets within independent containers, such as Toplevel windows, developers can enhance the overall user experience.

Updated on: 15-Feb-2024

2 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements