इस लेख में, हम एक GUI-based window resizer control panel बनाएंगे जिसमें window को उसकी ऊंचाई या चौड़ाई से resize करने के लिए pane होगा।
एप्लिकेशन बनाने के लिए, हम पहले एक slider बनाएंगे जो window size को resize करने में मदद करेगा। Sliders tkinter के ttk library में उपलब्ध हैं। हम पहले "ttk" import करेंगे। फिर, हम एक नई window launch करेंगे जिसे resize करने की आवश्यकता है।
आइए पहले notebook में सभी आवश्यक libraries import करें और sliders का उपयोग करके control bars design करें।
उदाहरण
# 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))
# 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()
Sliders और controls के लिए GUI बनाने के बाद, हम विभिन्न functions को परिभाषित करेंगे जिन्हें sliders और window controls में invoke किया जाएगा।
पहले, हम एक नई window खोलने के लिए एक function बनाएंगे जहां control movement दिखाई देगा। फिर, हम width, height, और both के लिए function परिभाषित करेंगे।
Functions परिभाषित करने के बाद, यह इस प्रकार होगा:
उदाहरण
# 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))
# 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.geometry("100x100")
# Change width
def change_width(x):
win1.geometry(f"{int(width_slider.get())}x{int(height_slider.get())}")
# Change height
def change_height(x):
win1.geometry(f"{int(width_slider.get())}x{int(height_slider.get())}")
# Change both width and height
def change_both(x):
win1.geometry(f"{int(both_slider.get())}x{int(both_slider.get())}")
# 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")
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, command=change_height, 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_width, 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()
आउटपुट
उपरोक्त code चलाने से एक window resizer बनेगा।
