Introduction
अब तक हमने ऐसे programs बनाए हैं जो terminal (command line) पर चलते हैं।
लेकिन real-world applications में हमें ऐसे programs की जरूरत होती है जिनमें buttons, windows और forms हों।
इसी प्रकार के programs बनाने के लिए GUI Programming का उपयोग किया जाता है।
GUI का मतलब है Graphical User Interface, जहाँ user mouse और keyboard की मदद से interact करता है।
GUI क्या होता है
GUI एक ऐसा interface होता है जिसमें user graphical elements (जैसे buttons, text box, window) के माध्यम से program से interact करता है।
GUI Programming क्यों जरूरी है
- user-friendly applications बनाने के लिए
- desktop software बनाने के लिए
- real-world applications develop करने के लिए
Python में GUI Libraries
Python में GUI बनाने के लिए कई libraries उपलब्ध हैं:
- Tkinter (सबसे आसान और built-in)
- PyQt
- Kivy
Beginners के लिए Tkinter सबसे suitable है।
Tkinter क्या है
Tkinter Python की built-in GUI library है, जिसकी मदद से हम window-based applications बना सकते हैं।
Basic Structure of Tkinter Program
import tkinter as tk
window = tk.Tk()
window.title("My App")
window.mainloop()
Explanation
Tk()→ window create करता हैtitle()→ window का title set करता हैmainloop()→ program को run करता है
GUI Elements (Widgets)
GUI में different elements को widgets कहा जाता है:
- Label → text दिखाने के लिए
- Button → action perform करने के लिए
- Entry → input लेने के लिए
Example: Simple Window
import tkinter as tk
window = tk.Tk()
window.title("Demo")
label = tk.Label(window, text="Hello Python")
label.pack()
window.mainloop()
Example: Button
import tkinter as tk
def click():
print("Button clicked")
window = tk.Tk()
btn = tk.Button(window, text="Click Me", command=click)
btn.pack()
window.mainloop()
Example: User Input
import tkinter as tk
window = tk.Tk()
entry = tk.Entry(window)
entry.pack()
def show():
print(entry.get())
btn = tk.Button(window, text="Submit", command=show)
btn.pack()
window.mainloop()
Layout Management
Widgets को arrange करने के लिए methods use होते हैं:
- pack() → simple layout
- grid() → table layout
- place() → exact position
Real-life Example: Login Form
import tkinter as tk
window = tk.Tk()
tk.Label(window, text="Username").pack()
user = tk.Entry(window)
user.pack()
tk.Label(window, text="Password").pack()
pwd = tk.Entry(window)
pwd.pack()
def login():
print("Login clicked")
tk.Button(window, text="Login", command=login).pack()
window.mainloop()
Important Points
- GUI user-friendly interface बनाता है
- Tkinter सबसे basic GUI library है
- widgets GUI के elements होते हैं
- mainloop() जरूरी होता है
Common mistakes
- mainloop() भूल जाना
- widget pack() न करना
- function को सही से connect न करना
Mini Program
import tkinter as tk
app = tk.Tk()
tk.Label(app, text="Welcome").pack()
tk.Button(app, text="Exit", command=app.quit).pack()
app.mainloop()
Summary
GUI Programming Python में user-friendly applications बनाने का तरीका है।
इसकी मदद से हम buttons, forms और windows वाले applications बना सकते हैं।
यह topic real-world software development के लिए बहुत important है।