Window Resizer python Tkinter mini project for beginners
A window that changes its height and width dynamically
Python Tkinter mini-project - Window Resizer
Let's create a python and Tkinter project to brush up on your technical skills. Creating projects is the best way of learning something faster.
First of all let's create a GUI window
from tkinter import *
root = Tk()
root.title("Tkinter Resizer - Yourname")
root.geometry('250x200')
root.configure(background="#c9db02")
root.mainloop()
Great. Now move to the next step i.e creating a Label to display text in our root window.
Creating Label text to display in the root window
# Displaying label text in the root window
Label(root, text="Width: ", font="courier 20 bold", bg='#c9db02').grid(row=0, column=0, pady=30)
Label(root, text="Height: ", font="courier 20 bold", bg='#c9db02').grid(row=1, column=0, pady=30)
Create slider to set height and width
# Scale silder provides graphical slider where we set the value of width and height
width = Scale(root, from_=100, to=800, command=resize_window, orient=HORIZONTAL, width=20, bg='#c9db02')
height = Scale(root, from_=100, to=800, command=resize_window, orient=HORIZONTAL, width=20, bg='#c9db02')
width.grid(row=0, column=1)
height.grid(row=1, column=1)
So far so good. Now let's create a new window that we controlled using a slider.
Write the below code after the fifth line or after root.configure()
Toplevel window or new window
#Creating new window
top = Toplevel()
top.configure(background="#0a3b2a")
top.title("Resizing....")
It's time to create a function that is able to take the value of the height and width of the new window
Write down below code after Toplevel()
A function that takes the value of width and height
# Function where we put width and height
def resize_window(*args):
top.geometry(f"{width.get()}x{height.get()}")
Congratulation. You made it
How the whole code looks like
from tkinter import *
root = Tk()
root.title("Tkinter Resizer - Sailendra")
root.geometry('250x200')
root.configure(background="#c9db02")
# Creating new window
top = Toplevel()
top.configure(background="#0a3b2a")
top.title("Resizing....")
# Function where we put width and height
def resize_window(*args):
top.geometry(f"{width.get()}x{height.get()}")
# Displaying label text in the root window
Label(root, text="Width: ", font="courier 20 bold", bg='#c9db02').grid(row=0, column=0, pady=30)
Label(root, text="Height: ", font="courier 20 bold", bg='#c9db02').grid(row=1, column=0, pady=30)
# Scale silder provides graphical slider where we set the value of width and height
width = Scale(root, from_=100, to=800, command=resize_window, orient=HORIZONTAL, width=20, bg='#c9db02')
height = Scale(root, from_=100, to=800, command=resize_window, orient=HORIZONTAL, width=20, bg='#c9db02')
width.grid(row=0, column=1)
height.grid(row=1, column=1)
root.mainloop()
Thanks for reading. If I made any mistake feel free to reach out to me on Twitter. I will be happy to hear from you.
If you found it helpful, you can consider following at TWITTER