现在,我正在制作一个名为“学生信息”的GUI,您可以在其中添加学生姓名,姓氏,电话号码,年份,地址,还可以通过从tableWidget中选择ID来删除ID,还可以更新学生姓名,姓氏,电话号码,年份,地址。这就是我所做的所有功能。现在,我正在尝试使用搜索功能,在其中您可以按姓名和姓氏搜索学生,并在qlineedit_name,qlineedit_surname,qlineedit_phonenumber等中显示学生的姓名,姓氏,电话号码,年份,地址等。但是即使我不这样做,我也无法使用不知道如何实现该功能。
在代码中,有些东西是用我的语言编写的,所以不必太在意,应该重点放在如何使搜索功能上
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import *
from sqlitehelper import *
app = QtWidgets.QApplication([])
dlg = uic.loadUi("test2.ui")
helper = sqlitehelper("Data.db")
helper.createtable()
def loadData():
users = helper.select("SELECT * FROM users")
for row_number,user in enumerate(users):
dlg.tableWidget.insertRow(row_number)
for column_number,data in enumerate(user):
cell = QtWidgets.QTableWidgetItem(str(data))
dlg.tableWidget.setItem(row_number,column_number,cell)
def clearData():
dlg.tableWidget.clearSelection()
while(dlg.tableWidget.rowCount()>0):
dlg.tableWidget.removeRow(0)
dlg.tableWidget.clearSelection()
def autorefresh():
clearData()
loadData()
def add_user():
name = dlg.lineEdit.text()
surname = dlg.lineEdit_2.text()
fathersname = dlg.lineEdit_3.text()
year = dlg.lineEdit_4.text()
try:
if name.strip(" ") != "" and surname.strip(" ") != "" and year.strip(" ") != "" :
user = (name,surname,fathersname,int(year))
helper.insert("INSERT INTO users (name,surname,fathersname,year) VALUES(?,?,?,?)",user,)
autorefresh()
dlg.lineEdit.setText(name)
dlg.lineEdit_2.setText(surname)
dlg.lineEdit_3.setText(fathersname)
dlg.lineEdit_4.setText(year)
else:
show_message("Hoho")
except Exception:
show_message1("Ýalňyşlyk!")
def delete_user():
id_delete = get_selected_user_by_id()
helper.delete("DELETE FROM users WHERE id="+id_delete)
autorefresh()
def update_user():
id_update = get_selected_user_by_id()
name = dlg.lineEdit.text()
surname = dlg.lineEdit_2.text()
fathersname = dlg.lineEdit_3.text()
year = dlg.lineEdit_4.text()
try:
if name.strip(" ") != "" and surname.strip(" ") != "" and year.strip(" ") != "" :
user = (name,surname,fathersname,int(year))
helper.update("UPDATE users SET name=?,surname=?,fathersname=?,year=? WHERE id="+id_update,user)
autorefresh()
dlg.lineEdit.setText(name)
dlg.lineEdit_2.setText(surname)
dlg.lineEdit_3.setText(fathersname)
dlg.lineEdit_4.setText(year)
else:
show_message("Yalnyshlyk")
except Exception:
show_message("Ýalňyşlyk")
show_message1("Ýalňyşlyk!")
def selection_changed():
selected_row_by_id = get_selected_row_by_id()
selected_user_by_id = get_selected_user_by_id()
name = dlg.tableWidget.item(selected_row_by_id,1).text()
surname = dlg.tableWidget.item(selected_row_by_id,2).text()
fathersname = dlg.tableWidget.item(selected_row_by_id,3).text()
year = dlg.tableWidget.item(selected_row_by_id,4).text()
dlg.lineEdit.setText(name)
dlg.lineEdit_2.setText(surname)
dlg.lineEdit_3.setText(fathersname)
dlg.lineEdit_4.setText(year)
def get_selected_user_by_id():
return dlg.tableWidget.item(get_selected_row_by_id(), 0).text()
def get_selected_row_by_id():
return dlg.tableWidget.currentRow()
def show_message1(Title = "Ýalňyşlyk", message = "Ýyla diňe san girizmeli"):
QMessageBox.information(None,Title,message)
def show_message(Title = "Ýalňyşlyk", message = "Hökman ADY, FAMILIÝASY, DOGLAN ÝYLY girizilmeli"):
QMessageBox.information(None,Title,message)
dlg.pushButton.clicked.connect(add_user)
dlg.pushButton_2.clicked.connect(delete_user)
dlg.pushButton_3.clicked.connect(update_user)
dlg.tableWidget.itemSelectionChanged.connect(selection_changed)
loadData()
dlg.show()
app.exec()
我正在尝试提供搜索功能,在其中可以按姓名和姓氏搜索学生,并在qlineedit_name,qlineedit_surname,qlineedit_phonenumber等中显示学生的姓名,姓氏,电话号码,年份,地址。