动态调用Qt designer设计的UI界面(Dynamically call the UI interface designed by Qt designer)
下面展示一些 内联代码片
。
from PySide2.QtWidgets import QApplication, QMessageBox
from PySide2.QtUiTools import QUiLoader
import pymysql,re
import cv2 as cv
from timeit import default_timer as timer
import time
class Stats:
def __init__(self):
self.ui = QUiLoader().load('LED_SIMULATION.ui') #dynamic call
self.ui.actionbegin.triggered.connect(self.begin)
self.ui.statusBar().showMessage("Ready")
#Connect to the database function-----------------------------------
def ConnectMysql(self):
try:
if(self.dbnum==0):
self.db=pymysql.connect(host='localhost',user='root',password='root',database='robot',port=3306)
except Exception as e:
print(e)
self.ui.statusBar().showMessage("Database connection failed")
#Connect to the database function-----------------------------------
# Get weed detection status-----------------------------------
def GetStatus(self):
cur = self.db.cursor()
sql = "select * from detector_status"
try:
cur.execute(sql)
status =cur.fetchone()
#status = status[4]
print(status)
return status[1], status[2], status[3], status[4],
except Exception as e:
print(e)
# Get weed detection status-----------------------------------
# Fill the corresponding area in red according to the database information-------
def simulation_Led(self,column_num, status):
if (status == 1):
if (column_num == "column_one"):
self.ui.label_1.setStyleSheet("QLabel{background-color:rgb(255,0,0);}")
self.ui.statusBar().showMessage("The spraying area numbered (1) began to spray herbicides")
elif (column_num == "column_two"):
self.ui.label_2.setStyleSheet("QLabel{background-color:rgb(255,0,0);}")
self.ui.statusBar().showMessage("The spraying area numbered (2) began to spray herbicides")
elif (column_num == "column_three"):
self.ui.label_3.setStyleSheet("QLabel{background-color:rgb(255,0,0);}")
self.ui.statusBar().showMessage("The spraying area numbered (3) began to spray herbicides")
elif (column_num == "column_four"):
self.ui.label_4.setStyleSheet("QLabel{background-color:rgb(255,0,0);}")
self.ui.statusBar().showMessage("The spraying area numbered (4) began to spray herbicides")
else:
if (column_num == "column_one"):
self.ui.label_1.setStyleSheet("QLabel{background-color:rgb(240,240,240);}")
self.ui.statusBar().showMessage("Stop spraying herbicides in the spraying area numbered (1)")
elif (column_num == "column_two"):
self.ui.label_2.setStyleSheet("QLabel{background-color:rgb(240,240,240);}")
self.ui.statusBar().showMessage("Stop spraying herbicides in the spraying area numbered (2)")
elif (column_num == "column_three"):
self.ui.label_3.setStyleSheet("QLabel{background-color:rgb(240,240,240);}")
self.ui.statusBar().showMessage("Stop spraying herbicides in the spraying area numbered (3)")
elif (column_num == "column_four"):
self.ui.label_4.setStyleSheet("QLabel{background-color:rgb(240,240,240);}")
self.ui.statusBar().showMessage("Stop spraying herbicides in the spraying area numbered (4)")
# Fill the corresponding area in red according to the database information-------
def begin(self):
self.dbnum = 0
self.ConnectMysql()
while True:
time.sleep(0.1)
start = timer()
num_1, num_2, num_3, num_4 = self.GetStatus()
print(num_1, num_2, num_3, num_4)
self.simulation_Led("column_one", num_1)
self.simulation_Led("column_two", num_2)
self.simulation_Led("column_three", num_3)
self.simulation_Led("column_four", num_4)
end = timer()
print(end - start)
if cv.waitKey(20) & 0XFF == ord('q'):
break
self.db.close()
self.ui.statusBar().showMessage("Program stopped")
app = QApplication([])
stats = Stats()
stats.ui.show()
app.exec_()