Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Window Resizer Control Panel in Tkinter
In this article, we will create a GUI-based window resizer control panel using Tkinter that allows you to resize a target window by adjusting its width and height through sliders.
We'll use the ttk library from Tkinter to create the sliders and build a control panel that can launch a new window and dynamically resize it using three different controls: width-only, height-only, and proportional resizing.
Creating the Basic Control Panel
Let's start by creating the main window with the control interface −
# Import the required Libraries
from tkinter import *
from tkinter import ttk
# Create Object
win = Tk()
# Set title
win.title("Window Resizer")
lab = Label(win, text="Window Resizer", font=('Poppins bold', 20))
lab.pack(pady=10)
# Define the geometry for the window or frame
win.geometry("500x500")
# Create a button to launch a new window
launch_button = Button(win, text="Launch")
launch_button.pack(pady=10)
# Add Label Frames for width, height and both
width_frame = LabelFrame(win, text="Width")
width_frame.pack(pady=10)
height_frame = LabelFrame(win, text="Height")
height_frame.pack(pady=10)
both_frame = LabelFrame(win, text="Both")
both_frame.pack(pady=10)
# Width Slider
width_slider = ttk.Scale(width_frame, from_=100, to=500, orient=HORIZONTAL, length=200, value=100)
width_slider.pack(pady=10, padx=20)
# Height Slider
height_slider = ttk.Scale(height_frame, from_=100, to=500, orient=VERTICAL, length=200, value=100)
height_slider.pack(pady=10, padx=20)
# Both Slider
both_slider = ttk.Scale(both_frame, from_=100, to=500, orient=HORIZONTAL, length=200, value=100)
both_slider.pack(pady=10, padx=20)
# Keep running the window
win.mainloop()
Complete Window Resizer with Functions
Now we'll add the functionality to actually launch a new window and control its resizing −
# Import the required Libraries
from tkinter import *
from tkinter import ttk
# Create Object
win = Tk()
# Set title
win.title("Window Resizer")
lab = Label(win, text="Window Resizer", font=('Poppins bold', 20))
lab.pack(pady=10)
# Define the geometry for the window or frame
win.geometry("500x500")
# Define Functions for all different events
# Open New Window
def launch_win():
global win1
win1 = Toplevel()
win1.title("Target Window")
win1.geometry("100x100")
# Change width only
def change_width(x):
current_height = win1.winfo_height()
win1.geometry(f"{int(width_slider.get())}x{current_height}")
# Change height only
def change_height(x):
current_width = win1.winfo_width()
win1.geometry(f"{current_width}x{int(height_slider.get())}")
# Change both width and height proportionally
def change_both(x):
size = int(both_slider.get())
win1.geometry(f"{size}x{size}")
# Create a button to launch a new window
launch_button = Button(win, text="Launch", command=launch_win)
launch_button.pack(pady=10)
# Add Label Frames for width, height and both
width_frame = LabelFrame(win, text="Width Control")
width_frame.pack(pady=10)
height_frame = LabelFrame(win, text="Height Control")
height_frame.pack(pady=10)
both_frame = LabelFrame(win, text="Proportional Control")
both_frame.pack(pady=10)
# Width Slider
width_slider = ttk.Scale(width_frame, from_=100, to=500, orient=HORIZONTAL, length=200, command=change_width, value=100)
width_slider.pack(pady=10, padx=20)
# Height Slider
height_slider = ttk.Scale(height_frame, from_=100, to=500, orient=VERTICAL, length=200, command=change_height, value=100)
height_slider.pack(pady=10, padx=20)
# Both Slider
both_slider = ttk.Scale(both_frame, from_=100, to=500, orient=HORIZONTAL, length=200, command=change_both, value=100)
both_slider.pack(pady=10, padx=20)
# Keep Running the window or frame
win.mainloop()
How It Works
The application consists of several key components:
-
Launch Button: Creates a new
Toplevel()window that serves as the target for resizing - Width Slider: Controls only the width of the target window while preserving height
- Height Slider: Controls only the height of the target window while preserving width
- Proportional Slider: Changes both width and height to the same value, creating a square window
Each slider uses the command parameter to bind a callback function that executes whenever the slider value changes. The geometry() method is used to resize the target window with the format "widthxheight".
Output
Running the above code will create a window resizer control panel. Click "Launch" to open the target window, then use the sliders to dynamically resize it.
Conclusion
This window resizer control panel demonstrates how to use Tkinter's ttk.Scale widgets to create interactive controls. The combination of Toplevel() windows and callback functions provides real-time window resizing functionality.
