← Back to Python Course | Chapter 14: Advanced Python & Tools | Lesson 13 of 15

Python tkinter Widgets

Widgets वे टुकड़े हैं जिन्हें आप किसी window में डालते हैं, जैसे labels, buttons और text boxes, बिल्कुल किसी app के लिए LEGO bricks की तरह। आप इन्हें जहाँ चाहें वहाँ arrange करते हैं।
Syntax
python
widget = tk.Button(root, text="text", command=function_name)
widget.pack()

Labels जोड़ना

Label widget window पर static text (या एक image) दिखाता है और आमतौर पर शुरू करने के लिए सबसे सरल widget है, क्योंकि इसकी अपनी कोई interactivity नहीं होती -- बस ऐसा content जो user को दिखाया जाता है।

उदाहरण: Adding Labels

python
import tkinter
root = tkinter.Tk()
label = tkinter.Label(root, text="Hello")  # displays static text, no interactivity
label.pack()
print(label["text"])

Action Buttons जोड़ना

Button widget एक clickable button render करता है, और इसके command parameter में कोई function पास करना उस function को button click होने पर चलने के लिए जोड़ देता है -- यही callback pattern है जिससे किसी Tkinter app में ज़्यादातर interactivity trigger होती है।

उदाहरण: Adding Action Buttons

python
import tkinter

def on_click():
    print("Button clicked")

root = tkinter.Tk()
button = tkinter.Button(root, text="Click me", command=on_click)  # on_click runs when clicked
button.pack()
button.invoke()  # simulates a click for this example

pack से Widgets रखना

pack() geometry manager widgets को window में एक के बाद एक रखता है, default रूप से उन्हें vertically stack करते हुए (या side=left से side-by-side), और बिना exact coordinates के खुद को उन widgets के हिसाब से size कर लेता है जो उसमें हैं।

उदाहरण: Placing Widgets with pack

python
import tkinter
root = tkinter.Tk()
tkinter.Label(root, text="One").pack()  # stacked vertically by default
tkinter.Label(root, text="Two").pack(side="left")  # placed to the left instead
print("Widgets stacked with pack()")

grid से Positioning

grid() geometry manager widgets को किसी spreadsheet की तरह row-and-column layout में arrange करता है, जो pack() के सरल stacking behavior से ज़्यादा बारीक control देता है widget की सटीक position पर -- हालाँकि एक ही container में pack() और grid() को मिलाने से errors आते हैं, इसलिए किसी window को इनमें से एक ही पर टिके रहना चाहिए।

उदाहरण: Positioning with grid

python
import tkinter
root = tkinter.Tk()
tkinter.Label(root, text="Row 0").grid(row=0, column=0)  # row/column layout, like a spreadsheet
tkinter.Label(root, text="Row 1").grid(row=1, column=0)
print("Widgets arranged with grid()")

Entry से Input Fields

Entry widget एक single-line text input box देता है जहाँ user type कर सकता है, और उस पर .get() call करने से अभी entered text मिल जाता है -- Tkinter form से user input पढ़ने का standard तरीका।

उदाहरण: Input Fields with Entry

python
import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root)  # single-line text input box
entry.insert(0, "Alex")
entry.pack()
print(entry.get())  # reads the current text in the box
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.