बहुत बढ़िया! 🎉 Lesson 8 पूरी तरह सफल है।
आपके screenshot में सबसे महत्वपूर्ण result है:
[(1, 'Rahul Kumar', 'Ramesh Kumar', '9876543210', 'V', 1000.0)]इसका मतलब:
id→ 1Student Name → Rahul Kumar
Father Name → Ramesh Kumar
Mobile → 9876543210
Class → V
Monthly Fee → ₹1000
और सबसे महत्वपूर्ण बात:
Rahul Kumar का record अब
school.dbSQLite database में वास्तव में save हो चुका है। ✅
अब हम अगले चरण पर जाएँगे।
🎓 Lesson 9 — Database से Student List बनाना
हमने पिछला lesson यहाँ तक पूरा कर लिया है:
Student Form ↓ Save Student ↓ SQLite Database ↓ school.db
अब अगला सवाल है:
Database में saved students को software में कैसे दिखाएँ?
अभी Rahul Kumar database में है, लेकिन application में उसे देखने का कोई proper तरीका नहीं है।
हम बनाएँगे:
┌──────────────────────────────────────────────────────┐ │ Student Admission Form │ │ │ │ Student Name: [ ] │ │ Father Name: [ ] │ │ Mobile: [ ] │ │ Class: [ ] │ │ Monthly Fee: [ ] │ │ │ │ [ Save Student ] │ │ │ ├──────────────────────────────────────────────────────┤ │ ID │ Student Name │ Father Name │ Mobile │ Class │ Fee│ ├────┼──────────────┼─────────────┼────────┼───────┼───┤ │ 1 │ Rahul Kumar │ Ramesh Kumar│987... │ V │1000│ └────┴──────────────┴─────────────┴────────┴───────┴───┘
इसके लिए हम QTableWidget सीखेंगे।
Step 1 — QTableWidget क्या है?
PySide6 में:
QTableWidget
एक ऐसी table बनाने के लिए इस्तेमाल होता है जिसमें हम rows और columns में data दिखा सकते हैं।
उदाहरण:
ID | Name | Class | Fee ------------------------ 1 | Rahul| V |1000 2 | Amit | VI |1200
यही हमारे Student Management में बहुत काम आएगा।
Step 2 — QTableWidget को Import करें
अपने main.py के सबसे ऊपर आपको यह section मिलेगा:
from PySide6.QtWidgets import ( QApplication, QMainWindow, QWidget, QLabel, QPushButton, QVBoxLayout, QFormLayout, QMessageBox, QLineEdit )
इसमें नीचे दो चीजें जोड़ें:
QTableWidget, QTableWidgetItem
अब:
from PySide6.QtWidgets import ( QApplication, QMainWindow, QWidget, QLabel, QPushButton, QVBoxLayout, QFormLayout, QMessageBox, QLineEdit, QTableWidget, QTableWidgetItem )
ये दोनों क्यों?
QTableWidget → पूरी table बनाएगा।
QTableWidgetItem → table के अंदर individual value रखेगा।
Step 3 — Student Table बनाना
अब हमें application के अंदर table बनानी है।
आपके code में यह हिस्सा है:
# Save Button save_button = QPushButton( "Save Student" ) save_button.clicked.connect( save_student ) main_layout.addWidget( save_button )
इसके नीचे अभी यह code लिखें:
# Student Table student_table = QTableWidget() student_table.setColumnCount(6) student_table.setHorizontalHeaderLabels([ "ID", "Student Name", "Father Name", "Mobile", "Class", "Monthly Fee" ]) main_layout.addWidget(student_table)
🧠 अब समझिए कि हमने क्या किया
यह:
student_table = QTableWidget()
एक खाली table बनाता है।
यह:
student_table.setColumnCount(6)
कहता है:
हमारी table में 6 columns होंगे।
क्योंकि हमें चाहिए:
1. ID 2. Student Name 3. Father Name 4. Mobile 5. Class 6. Monthly Fee
यह:
student_table.setHorizontalHeaderLabels(...)
ऊपर headings बनाता है।
Result:
ID | Student Name | Father Name | Mobile | Class | Monthly Fee
और यह:
main_layout.addWidget(student_table)
table को हमारी main window में डाल देता है।
Step 4 — Database से Data निकालना
अब हमारे पास table तो बन गई, लेकिन अभी खाली है।
हमें database से students पढ़ने होंगे।
इसके लिए save_student() से पहले एक नया function बनाएँ:
def load_students():
यह function क्या करेगा?
SQLite database से students निकालेगा और table में दिखाएगा।
Step 5 — Database से Students SELECT करें
Function के अंदर:
def load_students(): connection = get_connection() cursor = connection.cursor() cursor.execute(""" SELECT id, student_name, father_name, mobile, class_name, monthly_fee FROM students ORDER BY id DESC """) students = cursor.fetchall() connection.close()
अब समझते हैं।
SELECT
SELECT id, student_name, ... FROM students
का मतलब:
studentstable से ये information निकालो।
fetchall()
students = cursor.fetchall()
का मतलब:
Database से मिले सभी students को Python में ले आओ।
हमारे database में अभी Rahul है:
[ (1, 'Rahul Kumar', 'Ramesh Kumar', '9876543210', 'V', 1000.0) ]
Step 6 — Database Records को Table में डालना
अब उसी load_students() function में नीचे लिखें:
student_table.setRowCount(len(students)) for row_number, student in enumerate(students): for column_number, value in enumerate(student): item = QTableWidgetItem(str(value)) student_table.setItem( row_number, column_number, item )
यह हिस्सा थोड़ा बड़ा है, इसलिए इसे समझना जरूरी है।
setRowCount
student_table.setRowCount(len(students))
अगर database में:
1 student
है तो table में 1 row बनेगी।
अगर:
10 students
हैं तो 10 rows बनेंगी।
पहला for
for row_number, student in enumerate(students):
एक-एक student को पढ़ता है।
दूसरा for
for column_number, value in enumerate(student):
उस student की एक-एक information पढ़ता है।
उदाहरण:
student │ ├── 1 ├── Rahul Kumar ├── Ramesh Kumar ├── 9876543210 ├── V └── 1000
QTableWidgetItem
item = QTableWidgetItem(str(value))
Database की value को table item में बदलता है।
फिर:
student_table.setItem(...)
उसे सही row और column में रख देता है।
Step 7 — load_students() कब चलाना है?
अब हमें function को call करना होगा।
लेकिन ध्यान रखें:
student_tableबनने के बादload_students()चलना चाहिए।
इसलिए सबसे नीचे, जहाँ आपने:
student_table = QTableWidget()
से table बनाई है और:
main_layout.addWidget(student_table)
कर दिया है, उसके बाद लिखें:
load_students()
अब application शुरू होते ही database के पुराने students table में आ जाएँगे।
Step 8 — Save करने के बाद भी Table Refresh करें
एक और समस्या है।
मान लीजिए application खुली और Rahul पहले से दिखाई दे रहा है।
अब आप Amit को save करते हैं।
Amit database में तो चला जाएगा, लेकिन table अपने-आप update नहीं होगी।
इसलिए save_student() के अंत में success message के बाद:
load_students()
लिखेंगे।
उदाहरण:
QMessageBox.information( window, "Success", "Student saved successfully!" ) load_students()
अब:
Save Student ↓ Database में Save ↓ load_students() ↓ Table Refresh
⚠️ एक महत्वपूर्ण Python बात
save_student() के अंदर load_students() इस्तेमाल हो रहा है।
इसलिए हमारा function order ऐसा रखना बेहतर है:
get_connection() ↓ create_table() ↓ load_students() ↓ save_student()
यानी load_students() को save_student() से पहले define करें।
🧪 अब Test करें
Application चलाएँ:
python main.py
आपके school.db में Rahul पहले से है।
इसलिए application खुलते ही Rahul table में दिखाई देना चाहिए।
फिर नया student डालें:
Student Name: Amit Kumar Father Name: Suresh Kumar Mobile: 9123456789 Class: VI Monthly Fee: 1200
Save Student दबाएँ।
Table में दोनों दिखने चाहिए:
ID | Student Name | Father Name | Mobile | Class | Monthly Fee -------------------------------------------------------------------- 2 | Amit Kumar | Suresh Kumar | 9123456789 | VI | 1200 1 | Rahul Kumar | Ramesh Kumar | 9876543210 | V | 1000
📌 अगर Editing में कहीं परेशानी हो जाए
जैसा आपने कहा, इसलिए नीचे पूरा current main.py भी दे रहा हूँ। इसे अभी पहले copy करने की जरूरत नहीं है। पहले ऊपर बताए steps से खुद बदलाव करने की कोशिश करें।
अगर कहीं गलती हो जाए, तो इस पूरे code से compare कर सकते हैं।
import sqlite3import sysfrom PySide6.QtWidgets import (QApplication,QMainWindow,QWidget,QLabel,QPushButton,QVBoxLayout,QFormLayout,QMessageBox,QLineEdit,QTableWidget,QTableWidgetItem)def get_connection():return sqlite3.connect("school.db")def create_table():connection = get_connection()cursor = connection.cursor()cursor.execute("""CREATE TABLE IF NOT EXISTS students (id INTEGER PRIMARY KEY AUTOINCREMENT,student_name TEXT NOT NULL,father_name TEXT NOT NULL,mobile TEXT,class_name TEXT,monthly_fee REAL)""")connection.commit()connection.close()def load_students():connection = get_connection()cursor = connection.cursor()cursor.execute("""SELECT id, student_name, father_name,mobile, class_name, monthly_feeFROM studentsORDER BY id DESC""")students = cursor.fetchall()connection.close()student_table.setRowCount(len(students))for row_number, student in enumerate(students):for column_number, value in enumerate(student):item = QTableWidgetItem(str(value))student_table.setItem(row_number,column_number,item)def save_student():name = student_name.text()father = father_name.text()mobile_number = mobile.text()student_class_value = student_class.text()fee = monthly_fee.text()connection = get_connection()cursor = connection.cursor()cursor.execute("""INSERT INTO students (student_name,father_name,mobile,class_name,monthly_fee)VALUES (?, ?, ?, ?, ?)""", (name,father,mobile_number,student_class_value,fee))connection.commit()connection.close()QMessageBox.information(window,"Success","Student saved successfully!")load_students()create_table()app = QApplication(sys.argv)window = QMainWindow()window.setWindowTitle("School Fee Management Software")window.resize(900, 600)# Main Widgetcentral_widget = QWidget()window.setCentralWidget(central_widget)# Main Layoutmain_layout = QVBoxLayout()central_widget.setLayout(main_layout)# titletitle = QLabel("Student Admission Form")main_layout.addWidget(title)# Form Layoutform_layout = QFormLayout()# Student Fieldsstudent_name = QLineEdit()student_name.setPlaceholderText("Enter student name")father_name = QLineEdit()father_name.setPlaceholderText("Enter father name")mobile = QLineEdit()mobile.setPlaceholderText("Enter mobile number")student_class = QLineEdit()student_class.setPlaceholderText("Enter class")monthly_fee = QLineEdit()monthly_fee.setPlaceholderText("Enter monthly fee")# Add fields to formform_layout.addRow("Student Name:", student_name)form_layout.addRow("Father Name:", father_name)form_layout.addRow("Mobile Number:", mobile)form_layout.addRow("Class:", student_class)form_layout.addRow("Monthly Fee:", monthly_fee)# Add form to main layoutmain_layout.addLayout(form_layout)# Save Buttonsave_button = QPushButton("Save Student")save_button.clicked.connect(save_student)main_layout.addWidget(save_button)# Student Tablestudent_table = QTableWidget()student_table.setColumnCount(6)student_table.setHorizontalHeaderLabels(["ID","Student Name","Father Name","Mobile","Class","Monthly Fee"])main_layout.addWidget(student_table)load_students()window.show()sys.exit(app.exec())
अब से हम हर Lesson इसी तरीके से करेंगे। पहले मैं आपको समझाऊँगा कि क्या और कहाँ लिखना है, फिर पूरा code reference के लिए दूँगा। इससे आप धीरे-धीरे खुद software बनाना सीखेंगे, सिर्फ code copy नहीं करेंगे।
0 Comments
कमेंट केवल पोस्ट से रिलेटेड करें