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

Tkinter में Button Text Dynamically Resize कैसे करें?

मान लीजिए हमने Tkinter Frame में एक button और एक label बनाया है। Task है button text को dynamically resize करके इसके main window के अनुसार adjust करना। हम button widget का उपयोग करके button बना सकते हैं। हालांकि, button label dynamically बनाने के लिए कई अन्य functions उपयोग किए जाते हैं।

इस उदाहरण में, हम कुछ labels के साथ दो buttons बनाएंगे। Grid method जैसे rowconfigure() और columnconfigure() का उपयोग करके, हम dynamically main window या root को resize करेंगे।

Button text को dynamic बनाने के लिए, हम bind(<Configure>, command) method का उपयोग करेंगे जो functions और method को एक साथ bind करने में मदद करेगा। हम एक callback function pass कर सकते हैं जो generally widget के new location को refer करता है और एक helper function जो window size के अनुसार button text में changes modify करता है।

पहले, हम button text को इसकी width द्वारा resize करेंगे और फिर इसकी height द्वारा।

उदाहरण

from tkinter import *

win = Tk()
win.geometry("700x300")

# Dynamically resize the window and its widget
Grid.rowconfigure(win, index=0, weight=1)
Grid.columnconfigure(win, index=0, weight=1)

# Define the function to change the size of the button text
def resize(e):
    # Get the width of the button
    w = e.width / 10
    # Dynamically Resize the Button Text
    b.config(font=("Times New Roman", int(w)))
    # Resize the height
    if e.height <= 300:
        b.config(font=("Times New Roman", 30))
    elif e.height < 100:
        b.config(font=("Time New Roman", 10))

# Let us Create buttons
b = Button(win, text="Python")
b.grid(row=0, column=0, sticky="nsew")

win.bind('', resize)
win.mainloop()

आउटपुट

उपरोक्त code चलाने से "Python" text के साथ एक button बनेगा और यह button dynamically resized हो सकता है।

Tkinter में Button Text Dynamically Resize कैसे करें?

  1. Tkinter में टेक्स्ट विजेट से स्क्रॉलबार कैसे अटैच करें? Tkinter में टेक्स्ट विजेट से स्क्रॉलबार कैसे अटैच करें?

    Tkinter Text विजेट मल्टीलाइन यूज़र इनपुट स्वीकार करने के लिए उपयोग किया जाता है। यह एंट्री विजेट के समान है लेकिन एकमात्र अंतर यह है कि Text विजेट मल्टीलाइन टेक्स्ट सपोर्ट करता है। Text विजेट बनाने के लिए, हमें एक टेक्स्ट ऑब्जेक्ट इंस्टेंशिएट करना होगा। मल्टीपल टेक्स्ट जोड़ने के लिए ScrollBar जोड़न

  1. बटन दबाकर Tkinter विंडो कैसे बंद करें? बटन दबाकर Tkinter विंडो कैसे बंद करें?

    Tkinter शुरू में एक विंडो या फ्रेम बनाता है जिसमें उसमें विजेट्स और लेबल्स होते हैं। मान लीजिए हम एक बटन के साथ tkinter विंडो बंद करना चाहते हैं। बटन एक UI विजेट है जिसका उपयोग किसी निश्चित ऑपरेशन को करने के लिए किया जा सकता है। उदाहरण यहां, हम एक बटन बनाएंगे जो tkinter विंडो बंद करता है। TCL इंटर

  1. Tkinter में Image को Button के रूप में कैसे उपयोग करें? Tkinter में Image को Button के रूप में कैसे उपयोग करें?

    इस उदाहरण में, हम एक window में rounded button बनाएंगे जिसका उपयोग forms, games, dialogue boxes आदि जैसे कई अन्य एप्लिकेशन में किया जा सकता है। Tkinter में rounded buttons बनाने का सबसे अच्छा तरीका buttons की desired images का उपयोग करना और इसे frame में clickable button में बदलना है। यह PhotoImage(