Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

Tkinter में Window Resizer Control Panel

इस लेख में, हम एक 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 बनेगा।

Tkinter में Window Resizer Control Panel

  1. Tkinter Window Close कैसे करें? Tkinter Window Close कैसे करें?

    Tkinter का उपयोग करके एप्लिकेशन बनाना आसान है लेकिन कभी-कभी, title bar पर button के बिना window या frame को close करना मुश्किल हो जाता है। ऐसे cases में, हम window close करने के लिए .destroy() method का उपयोग कर सकते हैं। चूंकि tkinter attributes एक-दूसरे से independent हैं, हम button का उपयोग करके

  1. Python Tkinter में Transparent Window बनाएं Python Tkinter में Transparent Window बनाएं

    Python functional और desktop applications develop करने और बनाने के लिए सबसे लोकप्रिय भाषा है। इसमें अलग-अलग modules और functions की एक rich library है जो एप्लिकेशन बनाने और develop करने के लिए extensibility और accessibility प्रदान करती है। Tkinter GUI-based applications बनाने के लिए सबसे commonly us

  1. Python Tkinter में Frameless Window बनाएं Python Tkinter में Frameless Window बनाएं

    Tkinter GUI-based applications बनाने के लिए सबसे commonly used Python library है। इसमें widgets add करने और अन्य necessary attributes जैसी features हैं। मान लीजिए हम tkinter का उपयोग करके borderless window बनाना चाहते हैं। Borderless window बनाने के लिए, हम overrideredirect method का उपयोग कर सकते ह