from PySide6.QtWidgets import QWidget, QApplication, QFileDialog, QPushButton, QTableWidgetItem from Ui_试卷列表 import Ui_Form from database_tools_2 import * class MyWindow_sjlb(QWidget,Ui_Form): def __init__(self,database_name): super().__init__() self.database_name = database_name self.setupUi(self) self.bind() def setdbname(self,string): self.database_name = string try: self.db.close() except: pass self.db = connect(hostname=db_host, port=db_port, username= db_user, pwd= db_pwd, db=self.database_name) # print(self.database_name) def bind(self): self.tableWidget_papers.setColumnWidth(0,230) self.tableWidget_papers.setColumnWidth(1,120) self.tableWidget_papers.setColumnWidth(2,190) for self.button in self.findChildren(QPushButton): self.button.clicked.connect(self.generate_idexp) self.db = connect(hostname=db_host, port=db_port, username= db_user, pwd= db_pwd, db=self.database_name) def generate_idexp(self): self.tableWidget_papers.clearContents() self.tableWidget_papers.setRowCount(0) buttontext = self.sender().text() # print(buttontext) self.mycursor = self.db.cursor() self.mycursor.execute("SELECT name,idexp,remark FROM papers WHERE genre = %s AND NOT obsolete;",(buttontext,)) ret_list = self.mycursor.fetchall() self.tableWidget_papers.setRowCount(len(ret_list)) count = 0 for ret in ret_list: name,idexp,remark = ret self.tableWidget_papers.setItem(count,0,QTableWidgetItem(name)) self.tableWidget_papers.setItem(count,1,QTableWidgetItem(idexp)) self.tableWidget_papers.setItem(count,2,QTableWidgetItem(remark)) count += 1 if __name__ == '__main__': app = QApplication([]) windows = MyWindow_sjlb("tiku") windows.show() app.exec()