In this video we’ll create a fun little window resizer control panel using sliders.

We’ll create a width slider, a height slider, and a slider for both that will resize a second window in Tkinter.

Resizing windows is pretty easy, just set the geometry() manager to whatever height and width you want. This method allows us to resize windows dynamically based on where ever the slider happens to be at the moment.

Python Code: controls.py
(Github Code)

from tkinter import *
from tkinter import ttk

root = Tk()
root.title("Controls")
root.geometry("300x650")
root.iconbitmap('c:/guis/exe/codemy.ico')

# Create Launch Function
def launch():
	global second
	second = Toplevel()
	second.geometry("200x200")

# Change the width:
def width_slide(x):
	#int(width_slider.get())
	#int(height_slider.get())
	second.geometry(f"{int(width_slider.get())}x{int(height_slider.get())}")

# Change the height:
def height_slide(x):
	second.geometry(f"{int(width_slider.get())}x{int(height_slider.get())}")

# Change the both:
def both_slide(x):
	second.geometry(f"{int(both_slider.get())}x{int(both_slider.get())}")

# Create a launch button
launch_button = Button(root, text="Launch Window", command=launch)
launch_button.pack(pady=20)

# Create Some Label Frames
width_frame = LabelFrame(root, text="Width")
width_frame.pack(pady=20)

height_frame = LabelFrame(root, text="Height")
height_frame.pack(pady=20)

both_frame = LabelFrame(root, text="Both")
both_frame.pack(pady=20)

# Create Some Sliders
width_slider = ttk.Scale(width_frame, from_=100, to=500, orient=HORIZONTAL, length=200, command=width_slide, value=100)
width_slider.pack(pady=20, padx=20)

height_slider = ttk.Scale(height_frame, from_=100, to=500, orient=VERTICAL, length=200, command=height_slide, value=100)
height_slider.pack(pady=20, padx=20)

both_slider = ttk.Scale(both_frame, from_=100, to=500, orient=HORIZONTAL, length=200, command=both_slide, value=100)
both_slider.pack(pady=20, padx=20)



root.mainloop()

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of...