To resize a window in Python Tkinter on a button click, you can use the geometry
method to set the new dimensions of the window. Here’s an example code snippet that demonstrates how to resize a window in Tkinter:
import tkinter as tk
def resize_window():
"""
Resize the main window when the button is clicked.
"""
# Get the current width and height of the window
current_width = root.winfo_width()
current_height = root.winfo_height()
# Set the new width and height
new_width = current_width + 100
new_height = current_height + 100
# Update the window dimensions
root.geometry(f"{new_width}x{new_height}")
# Create the main window
root = tk.Tk()
# Create a button to resize the window
resize_button = tk.Button(root, text="Resize", command=resize_window)
resize_button.pack()
# Start the Tkinter event loop
root.mainloop()
In this example, we define the resize_window
function that will be called when the button is clicked. Inside the function, we use the winfo_width
and winfo_height
methods of the root window (root
) to get the current width and height of the window.
We then calculate the new width and height by adding a fixed value (e.g., 100) to the current dimensions. Finally, we use the geometry
method of the root window to set the new dimensions, using the format "widthxheight"
.
We create the main window using tk.Tk()
. Next, we create a button (resize_button
) with the text “Resize” and associate it with the resize_window
function using the command
parameter. We pack the button using resize_button.pack()
to display it in the window.
Finally, we start the Tkinter event loop using root.mainloop()
to run the application and handle user interactions.
When you run the code, you’ll see a window with a “Resize” button. Clicking the button will resize the window by increasing its width and height by 100 pixels. You can modify the code to adjust the resizing logic according to your requirements.
+ There are no comments
Add yours