Я новичок в Python GUI и использую pyqt4. На данный момент мой код имеет графический интерфейс, который имеет qtableWidget, и он показывает данные таблицы из базы данных sqlite3. Я хочу, чтобы пользователи могли редактировать таблицу, добавлять новые значения и сохранять ее. После нажатия кнопки сохранения данные также должны быть сохранены в базе данных, чтобы при перезагрузке программы отображалась обновленная таблица.
код -
from PyQt4 import QtCore, QtGui
import sqlite3
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def loadData(self,current_item):
connection = sqlite3.connect('savess.db')
query = "SELECT * FROM Table1"
result = connection.execute(query)
for row_number, row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.tableWidget.setItem(row_number,column_number, QtGui.QTableWidgetItem(str(data)))
connection.close()
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(556, 396)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(10, 80, 531, 201))
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(5)
self.tableWidget.setRowCount(5)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(4, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(4, item)
self.comboBox = QtGui.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(10, 40, 91, 22))
self.comboBox.setObjectName(_fromUtf8("comboBox"))
self.comboBox.addItem(_fromUtf8(""))
self.comboBox.addItem(_fromUtf8(""))
self.comboBox.addItem(_fromUtf8(""))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(230, 300, 93, 28))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(440, 40, 93, 28))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_2.clicked.connect(self.loadData)
self.comboBox_2 = QtGui.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(232, 40, 101, 22))
self.comboBox_2.setObjectName(_fromUtf8("comboBox_2"))
self.comboBox_2.addItem(_fromUtf8(""))
self.comboBox_2.addItem(_fromUtf8(""))
self.comboBox_2.addItem(_fromUtf8(""))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 556, 26))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "1", None))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("MainWindow", "2", None))
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("MainWindow", "3", None))
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("MainWindow", "4", None))
item = self.tableWidget.verticalHeaderItem(4)
item.setText(_translate("MainWindow", "5", None))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Name", None))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Number", None))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "Email", None))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "Department", None))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "Comment", None))
self.comboBox.setItemText(0, _translate("MainWindow", "Table1", None))
self.comboBox.setItemText(1, _translate("MainWindow", "Table2", None))
self.comboBox.setItemText(2, _translate("MainWindow", "Amulya", None))
self.pushButton.setText(_translate("MainWindow", "Save", None))
self.pushButton_2.setText(_translate("MainWindow", "load", None))
self.comboBox_2.setItemText(0, _translate("MainWindow", "Rudra", None))
self.comboBox_2.setItemText(1, _translate("MainWindow", "mlb", None))
self.comboBox_2.setItemText(2, _translate("MainWindow", "chota bheem", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
а это таблица значений
('Name','001','email','Department','comment'))
('Name','002','email','Department','comment'))
('Name','003','email','Department','comment'))
('Name','004','email','Department','comment'))
('Name','005','email','Department','comment'))
Пожалуйста, скажите мне, как можно отредактировать qtableWidget, внести изменения и сохранить изменения в базе данных, чтобы при повторном запуске кода отображалась обновленная таблица.