Python tkinter Widgets
In this page:
widget = tk.Button(root, text="text", command=function_name)
widget.pack()
Labels जोड़ना
Label widget window पर static text (या एक image) दिखाता है और आमतौर पर शुरू करने के लिए सबसे सरल widget है, क्योंकि इसकी अपनी कोई interactivity नहीं होती -- बस ऐसा content जो user को दिखाया जाता है।
उदाहरण: Adding Labels
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
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
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
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
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
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- Python PEP 8 Style Guide
- Python Debugging Techniques
- Python Testing with unittest
- Python Common Mistakes
- Python Interview Questions
- Python map() & filter()
- Python reduce()
- Python zip() & enumerate()
- Python sorted() & key Functions
- Python Comprehensions Advanced
- Python Turtle Graphics
- Python tkinter Introduction
- Python tkinter Widgets
- Python pygame Introduction
- Python Mini Projects