Python Tkinter क्या है? GUI, Window, Label, Button, Entry & Events in Hindi | RBSE Class 12
Tkinter क्या है?
Tkinter Python में graphical user interface (GUI) applications बनाने के लिए उपयोग किया जाने वाला standard interface है। इसकी मदद से window-based applications बनाए जा सकते हैं।
Tkinter की सहायता से हम Window, Label, Button, Entry, Text और अन्य GUI components का उपयोग कर सकते हैं।
GUI क्या है?
GUI का पूरा नाम Graphical User Interface है।
GUI में user application के साथ graphical elements जैसे buttons, text boxes, menus और windows के माध्यम से interact कर सकता है।
Tkinter Import कैसे करें?
import tkinter
Tkinter को commonly tk alias के साथ भी import किया जाता है।
import tkinter as tk
पहली Tkinter Window
सबसे basic Tkinter program में एक main window बनाई जाती है।
import tkinter as tk
root = tk.Tk()
root.title("My First GUI")
root.mainloop()
Code को समझें
| Code | उपयोग |
|---|---|
| import tkinter as tk | Tkinter module import करता है |
| tk.Tk() | Main window create करता है |
| title() | Window का title set करता है |
| mainloop() | GUI event loop चलाता है |
Window का Title बदलना
import tkinter as tk
root = tk.Tk()
root.title("RBSE Python")
root.mainloop()
Window का Size बदलना
Window का initial size निर्धारित करने के लिए geometry()
method का उपयोग किया जा सकता है।
import tkinter as tk
root = tk.Tk()
root.title("RBSE Python")
root.geometry("500x300")
root.mainloop()
Label क्या है?
Label widget का उपयोग window पर text या information display करने के लिए किया जाता है।
import tkinter as tk
root = tk.Tk()
label = tk.Label(
root,
text="Welcome to Python"
)
label.pack()
root.mainloop()
Label में Font Size
import tkinter as tk
root = tk.Tk()
label = tk.Label(
root,
text="Welcome to Python",
font=("Arial", 20)
)
label.pack()
root.mainloop()
Button क्या है?
Button widget user को किसी action को perform करने की सुविधा देता है।
import tkinter as tk
root = tk.Tk()
button = tk.Button(
root,
text="Click Me"
)
button.pack()
root.mainloop()
Button पर Function चलाना
Button के click होने पर किसी function को execute करने के लिए command option का उपयोग किया जा सकता है।
import tkinter as tk
def show_message():
print("Button clicked")
root = tk.Tk()
button = tk.Button(
root,
text="Click Me",
command=show_message
)
button.pack()
root.mainloop()
command=show_message में function को call करने के लिए
parentheses नहीं लगाए जाते। Tkinter button click होने पर function
को execute करता है।
Entry Widget क्या है?
Entry widget का उपयोग user से single-line text input लेने के लिए किया जाता है।
import tkinter as tk
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
root.mainloop()
Entry से Text प्राप्त करना
Entry में user द्वारा लिखे गए text को प्राप्त करने के लिए
get() method का उपयोग किया जा सकता है।
import tkinter as tk
def show_name():
name = entry.get()
print(name)
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
button = tk.Button(
root,
text="Show Name",
command=show_name
)
button.pack()
root.mainloop()
Entry का Text Delete करना
Entry से text हटाने के लिए delete() method का उपयोग किया
जा सकता है।
entry.delete(0, tk.END)
Entry में Text Insert करना
Entry में programmatically text डालने के लिए insert()
method का उपयोग किया जा सकता है।
entry.insert(0, "RBSE Python")
Text Widget क्या है?
Text widget का उपयोग multiple lines का text input या display करने के लिए किया जा सकता है।
import tkinter as tk
root = tk.Tk()
text_box = tk.Text(root)
text_box.pack()
root.mainloop()
Text Widget में Text Insert करना
text_box.insert(
"1.0",
"Welcome to RBSE Python"
)
यहाँ 1.0 का अर्थ Text widget की पहली line और पहली
character position से है।
Text Widget से Data प्राप्त करना
data = text_box.get("1.0", tk.END)
print(data)
Text Widget से Data Delete करना
text_box.delete("1.0", tk.END)
Messagebox क्या है?
Tkinter में user को information या message दिखाने के लिए messagebox का उपयोग किया जा सकता है।
import tkinter as tk
from tkinter import messagebox
def show_message():
messagebox.showinfo(
"Message",
"Welcome to Python"
)
root = tk.Tk()
button = tk.Button(
root,
text="Show Message",
command=show_message
)
button.pack()
root.mainloop()
pack() क्या है?
pack() Tkinter का एक geometry manager है। इसका उपयोग widgets को window में arrange करने के लिए किया जा सकता है।
label.pack()
grid() क्या है?
grid() widgets को rows और columns के रूप में arrange करने के लिए उपयोग किया जाता है।
import tkinter as tk
root = tk.Tk()
tk.Label(root, text="Name").grid(
row=0,
column=0
)
tk.Entry(root).grid(
row=0,
column=1
)
root.mainloop()
grid() का Practical Example
import tkinter as tk
root = tk.Tk()
tk.Label(root, text="Name").grid(
row=0,
column=0
)
name_entry = tk.Entry(root)
name_entry.grid(
row=0,
column=1
)
tk.Label(root, text="Marks").grid(
row=1,
column=0
)
marks_entry = tk.Entry(root)
marks_entry.grid(
row=1,
column=1
)
root.mainloop()
place() क्या है?
place() geometry manager widget को specified position पर रखने के लिए उपयोग किया जा सकता है।
label.place(x=50, y=50)
pack(), grid() और place()
| Geometry Manager | मुख्य उपयोग |
|---|---|
| pack() | Widgets को simple arrangement में रखने के लिए |
| grid() | Rows और columns में widgets रखने के लिए |
| place() | Specified position पर widget रखने के लिए |
Event क्या है?
GUI application में user द्वारा किया गया action जैसे button click, keyboard input या mouse action एक event हो सकता है।
Tkinter में events को handle करने के लिए functions और
bind() method का उपयोग किया जा सकता है।
bind() Method
किसी specific event को function से connect करने के लिए
bind() का उपयोग किया जा सकता है।
import tkinter as tk
def key_pressed(event):
print("Key pressed")
root = tk.Tk()
root.bind("<Key>", key_pressed)
root.mainloop()
Button Click Event
Button के सामान्य click action के लिए command option
एक सरल तरीका है।
def click():
print("Button clicked")
button = tk.Button(
root,
text="Click",
command=click
)
Checkbutton क्या है?
Checkbutton widget का उपयोग किसी option को select या deselect करने के लिए किया जा सकता है।
import tkinter as tk
root = tk.Tk()
var = tk.BooleanVar()
check = tk.Checkbutton(
root,
text="I agree",
variable=var
)
check.pack()
root.mainloop()
Radiobutton क्या है?
Radiobutton का उपयोग multiple options में से आमतौर पर एक option select करने के लिए किया जा सकता है।
import tkinter as tk
root = tk.Tk()
choice = tk.StringVar()
tk.Radiobutton(
root,
text="Python",
variable=choice,
value="Python"
).pack()
tk.Radiobutton(
root,
text="Java",
variable=choice,
value="Java"
).pack()
root.mainloop()
Combobox क्या है?
Combobox एक dropdown-style widget है। इसे Tkinter के
ttk module से उपयोग किया जा सकता है।
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
combo = ttk.Combobox(
root,
values=["Python", "Java", "C++"]
)
combo.pack()
root.mainloop()
Complete Practical Example — Student Form
नीचे एक simple student form का example है जिसमें Name और Marks input लेकर button click पर message display किया जाता है।
import tkinter as tk
from tkinter import messagebox
def submit():
name = name_entry.get()
marks = marks_entry.get()
messagebox.showinfo(
"Student Details",
"Name: " + name +
"\nMarks: " + marks
)
root = tk.Tk()
root.title("Student Form")
root.geometry("400x250")
tk.Label(
root,
text="Student Name"
).pack()
name_entry = tk.Entry(root)
name_entry.pack()
tk.Label(
root,
text="Marks"
).pack()
marks_entry = tk.Entry(root)
marks_entry.pack()
submit_button = tk.Button(
root,
text="Submit",
command=submit
)
submit_button.pack()
root.mainloop()
Tkinter के Important Widgets
| Widget | उपयोग |
|---|---|
| Label | Text या information display करने के लिए |
| Button | Action perform करने के लिए |
| Entry | Single-line input के लिए |