Online Computer Courses Classes and Training Program

हम Lesson 4 — Button Click → Python Function सीखेंगे।

 


बहुत बढ़िया! 🎉 Lesson 3 भी बिल्कुल सही चला है।

Screenshot में साफ दिखाई दे रहा है:

  • ✅ Welcome message आ रहा है

  • ✅ Get Started button बन गया है

  • ✅ Button नीचे पूरे available width में है

  • ✅ कोई error नहीं है

एक छोटी सी बात आपने अभी practically देख ली है: QVBoxLayout widgets को vertical order में रखता है, लेकिन default में widgets available space के अनुसार stretch भी हो सकते हैं। इसी वजह से आपका button नीचे काफी चौड़ा दिखाई दे रहा है। यह बिल्कुल सही है।


🎓 Lesson 4 — Button पर Click करने से Python Code चलाना

अब हम programming की एक बहुत महत्वपूर्ण concept सीखेंगे:

Signal और Slot

इसी concept से आगे हमारे software में ये सभी buttons काम करेंगे:

Add Student
Save Student
Edit Student
Delete Student
Generate Bill
Collect Fee
Print Receipt
Backup

1. पहले एक छोटा Function बनाएँ

main.py में इस line के ऊपर:

app = QApplication(sys.argv)

यह function लिखें:

def button_clicked():
    print("Get Started button clicked!")

तो शुरुआत कुछ ऐसी होगी:

import sys

from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QWidget,
    QLabel,
    QPushButton,
    QVBoxLayout
)


def button_clicked():
    print("Get Started button clicked!")


app = QApplication(sys.argv)

🧠 Function क्या है?

बहुत आसान भाषा में:

Function = किसी काम के लिए बनाया गया छोटा program block।

हमने बनाया:

def button_clicked():

और उसके अंदर:

print("Get Started button clicked!")

मतलब:

जब button_clicked() को बुलाया जाएगा, Terminal में यह message print होगा।


2. अब Button को Function से जोड़ें

आपके code में अभी यह है:

button = QPushButton("Get Started")
layout.addWidget(button)

इसके बीच में यह line डालें:

button.clicked.connect(button_clicked)

अब पूरा relevant हिस्सा:

# Button
button = QPushButton("Get Started")

button.clicked.connect(button_clicked)

layout.addWidget(button)

🧠 यह line बहुत महत्वपूर्ण है

button.clicked.connect(button_clicked)

इसे टुकड़ों में समझिए:

button

हमारा button।

.clicked

Button का clicked signal।

मतलब:

User ने button दबाया।

.connect()

इस signal को किसी function से जोड़ना।

button_clicked

वह function जिसे button दबने पर चलाना है।

इसलिए:

User Button दबाता है
        ↓
button.clicked
        ↓
connect()
        ↓
button_clicked()
        ↓
print(...)

3. अब Program चलाएँ

पहले पुरानी application बंद करें।

फिर Terminal में:

python main.py

Window खुलेगी।

अब:

Get Started

button दबाइए।

Button दबाने के बाद Terminal में दिखाई देगा:

Get Started button clicked!

🎉

अब आपका button सिर्फ दिखाई नहीं दे रहा, बल्कि काम भी कर रहा है।


🧠 यह हमारे Final Software में कहाँ काम आएगा?

यही concept आगे ऐसे इस्तेमाल होगा:

Add Student

[ Add Student ]
       ↓
open_student_form()

Save Student

[ Save Student ]
       ↓
save_student()
       ↓
SQLite Database

Collect Fee

[ Collect Fee ]
       ↓
collect_payment()
       ↓
Payment Database
       ↓
Due Recalculate

Generate Bill

[ Generate Bill ]
       ↓
generate_bill()
       ↓
Database
       ↓
PDF

यानी आज आपने जो छोटी सी line सीखी:

button.clicked.connect(button_clicked)

यही हमारे पूरे GUI software की basic working का एक बहुत महत्वपूर्ण हिस्सा है।


🎯 आज का अभ्यास

अभी सिर्फ यही करें:

  1. button_clicked() function जोड़ें।

  2. button.clicked.connect(button_clicked) जोड़ें।

  3. Program run करें।

  4. Get Started दबाएँ।

  5. Terminal में Get Started button clicked! दिखाई दे।

उसका screenshot भेजिए।

फिर हम Lesson 5 में सीखेंगे कि button दबाने पर सिर्फ Terminal में message दिखाने के बजाय एक proper popup/message box कैसे दिखाते हैं।

Post a Comment

0 Comments