Open In App

Window Resizer Control Panel – Tkinter

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Tkinter

Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications.

In this article, we will learn how to make a window resizer control panel that is used to resize the window size once it is initialized.

Approach:

  • We will create two windows; one window is a parent and another one is a child.
  • Add one button; when we click on a button it will open the child window.
  • The parent window contains three sliders; width, height, and both sliders.
  • As the slide value change, child geometry will change.

Let’s understand step by step implementation:-

Step 1: Create a Normal Tkinter window

Python3




# Import Library
from tkinter import *
 
# Create Object
root = Tk()
 
# Set title
root.title("Controls")
 
# Set Geometry
root.geometry("400x500")
 
# Execute Tkinter
root.mainloop()


 

 

Output:

 

 

Step 2: Add Button, Sliders & LabelFrame

 

Python3




# Import Library
from tkinter import *
from tkinter import ttk
 
# Create Object
root = Tk()
 
# Set title
root.title("Controls")
 
# Set Geometry
root.geometry("400x500")
 
# Make Button
launch_button = Button(root,
                       text = "launch Window")
launch_button.pack(pady = 10)
 
# Add Label Frames
width_frame = LabelFrame(root,
                         text = "Change width")
 
width_frame.pack(pady = 10)
 
height_frame = LabelFrame(root,
                          text = "change height")
height_frame.pack(pady = 10)
 
both_frame = LabelFrame(root,
                        text = "change both")
 
both_frame.pack(pady = 10)
 
# Add Scale bar
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 = ttk.Scale(height_frame,
                          from_ = 100, to = 500,
                          orient = VERTICAL,
                          length = 200, value = 100)
height_slider.pack(pady = 10, padx = 20)
 
both_slider = ttk.Scale(both_frame, from_ = 100,
                        to = 500, orient = HORIZONTAL,
                        length = 200, value = 100)
 
both_slider.pack(pady = 10,padx = 20)
 
# Execute Tkinter
root.mainloop()


 

 

Output:-

 

 

Step 3: Now we will create four functions; one function is for a button that will open the child window, and the other three for changing the geometry.

 

Use the Toplevel() method to create a child window.

 

Python3




# Open New Window
def launch():
    global second
    second = Toplevel()
    second.geometry("100x100")
 
# Change width
def width_slide(x):
    second.geometry(f"{int(width_slider.get())}x{int(height_slider.get())}")
 
# Change height
def height_slide(x):
    second.geometry(f"{int(width_slider.get())}x{int(height_slider.get())}")
 
# Change both width and height
def both_slide(x):
    second.geometry(f"{int(both_slider.get())}x{int(both_slider.get())}")


 

 

Below is the full implementation:

 

Python3




# Import Library
from tkinter import *
from tkinter import ttk
 
# Create Object
root = Tk()
# Set title
root.title("Controls")
# Set Geometry
root.geometry("400x500")
 
# Open New Window
def launch():
    global second
    second = Toplevel()
    second.geometry("100x100")
 
# Change width
def width_slide(x):
    second.geometry(f"{int(width_slider.get())}x{int(height_slider.get())}")
 
# Change height
def height_slide(x):
    second.geometry(f"{int(width_slider.get())}x{int(height_slider.get())}")
 
# Change both width and height
def both_slide(x):
    second.geometry(f"{int(both_slider.get())}x{int(both_slider.get())}")
 
# Make Button
launch_button = Button(root,
                       text = "launch Window",
                       command = launch)
 
launch_button.pack(pady = 10)
 
# Add Label Frames
width_frame = LabelFrame(root,
                         text = "Change width")
width_frame.pack(pady = 10)
 
height_frame = LabelFrame(root,
                          text = "change height")
height_frame.pack(pady = 10)
 
both_frame = LabelFrame(root,
                        text = "change both")
both_frame.pack(pady = 10)
 
# Add Scale bar
width_slider = ttk.Scale(width_frame,from_ = 100, to = 500,
                         orient = HORIZONTAL, length = 200,
                         command = width_slide,
                         value = 100)
 
width_slider.pack(pady = 10, 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 = 10, 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 = 10, padx = 20)
 
# Execute Tkinter
root.mainloop()


 

 

Output:

 

 



Last Updated : 31 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads