动态调用Qt designer设计的UI界面(Dynamically call the UI interface designed by Qt designer)

动态调用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_()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值