mport mysql.connector as ps class MysqlHelper: def __init__(self, host, port ,user, password, database, charset): self.host = host self.port=port self.user = user self.password = password self.database = database self.charset = charset self.db = None self.curs = None # 数据库连接 def open(self): try: self.db = ps.connect(host=self.host, port=self.port, user=self.user, password=self.password, database=self.database, charset=self.charset) self.curs = self.db.cursor() except Exception as e: print("连接数据库失败:"+str(e)) # 数据库关闭 def close(self): self.curs.close() self.db.close() # 数据增删改 def runSQL(self, sql): self.open() try: self.curs.execute(sql) self.db.commit() except Exception as e: print("runSQL运行[" + str(sql) + "]出现错误" + str(e)) self.db.rollback() self.close() # 数据查询,返回键名和对应数据组成的字典 def findDictionary(self, sql): self.open() try: self.curs.execute(sql) index = self.curs.description result = [] for res in self.curs.fetchall(): row = {} for i in range(len(index)): row[index[i][0]] = res[i] result.append(row) self.close() return result except Exception as e: print("find运行[" + str(sql) + "]出现错误" + str(e)) #根据sql语句查询出数据,不包含键名 def find(self, sql): self.open() try: self.curs.execute(sql) result = self.curs.fetchall() self.close() return result except Exception as e: print("find运行[" + str(sql) + "]出现错误" + str(e)) # 根据表格名查询键名 def findColumeName(self, tableName): self.open() try: sql = "select * from "+tableName; self.curs.execute(sql) index = self.curs.description self.curs.fetchall() self.close() result = [] for i in range(len(index)): result.append(index[i][0]) return result except Exception as e: print("find运行[" + str(sql) + "]出现错误" + str(e))
import MysqlHelper import time class retrospect: def __init__(self): global db db = MysqlHelper.MysqlHelper("127.0.0.1", 3306, "root", "123456", "test", "utf8") def test(self): start = time.clock() sql = "select * from firstState" result = db.findDictionary(sql) #print (result[0]) # for x in result: # print(x) end = time.clock() print('Running time: %s Seconds' % (end - start)) return result re = retrospect() re.test()
import threading import time import Retrospect class testThread(): # 方法一:将要执行的方法作为参数传给Thread的构造方法 def action(self,func): global re re=[] while(1): time.sleep(1) print ("123") result = func() if(re != result[0]): re=result[0] print(result[0]) def start(self): t = threading.Thread(target=self.action(Retrospect.retrospect().test))#python支持把函数名传进去运行 t.start() t=testThread() t.start()
import sys from PyQt5.QtWidgets import * from PyQt5.uic import loadUi from PyQt5.QtGui import QColor import Retrospect import threading import time class MainWindow(QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) loadUi("main.ui", self) self.screen = QDesktopWidget().screenGeometry() self.width = self.screen.width() self.height = self.screen.height() # self.table() self.tableWidget.verticalHeader().setVisible(False) # 隐藏垂直表头 # self.tableWidget.horizontalHeader().setDefaultSectionSize(300)#设置列宽 self.tableWidget.horizontalHeader().setStretchLastSection(1) # 设置填充满横向 self.tableWidget.horizontalHeader().setStyleSheet("QHeaderView::section{background:skyblue;}") self.tableWidget.verticalHeader().setDefaultSectionSize(90) # 设置默认行高 self.tableWidget.setHorizontalHeaderLabels(['Item', 'Value']) # 设置表头样式 self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers) # 设置不允许操作 # self.table = QTableWidget() def table(self): self.tableWidget.setColumnCount(2) self.tableWidget.setRowCount(6) def btn_click(self): try: self.table() re = Retrospect.retrospect().test() for x in re: print(x) data1 = ['State:', 'Total:', 'Pass:', 'Yield:', 'Test Time:', 'SN:'] data2 = ['Idle', '1', '2', '3', '4', ''] for i in range(6): newItem1 = QTableWidgetItem(data1[i]) self.tableWidget.setItem(i, 0, newItem1) newItem2 = QTableWidgetItem(data2[i]) self.tableWidget.setItem(i, 1, newItem2) if (i == 5): newItem1.setBackground(QColor(255, 255, 0)) # newItem2.setBackground(QColor(255, 255, 0)) # self.sta_state[j].editItem(newItem2) pass except Exception as e: print(str(e)) app = QApplication(sys.argv) w = MainWindow() w.show() sys.exit(app.exec_())