1

My program is AI based. I have seen all post regarding Pyqt set text in stackoverflow. I want to setText in lineEdit when Auto(AI) button is clicked.

def AI_click(self):
        #self.lineEdit.setText("YO")
        os.system('python detect_video.py --weights ./checkpoints/custom-416 --size 416 --model yolov4 --video 0 --plate')
        #self.lineEdit.setText(line1)

Basically AI button executing py file and it print string. How can I use another .py string in this GUI py file. When my program execute py file it's no longer remember self.lineEdit.setText(line1) and also string.

class Ui_Dialog(object):
def setupUi(self, Dialog,var):
    Dialog.setObjectName("Dialog")
    Dialog.resize(1120, 850)
    Dialog.setStyleSheet("QDialog#Dialog{\n""background-color: 
    qlineargradient(spread:pad,x1:0.0113636, y1:0.966, x2:1, y2:0, 
    stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255))\n"
    "\n"
    "}\n"
    "\n"
    "")
    self.widget = QtWidgets.QWidget(Dialog)
    self.widget.setGeometry(QtCore.QRect(0, 0, 201, 851))
    self.widget.setStyleSheet("\n"
    "background-color: rgb(100, 100, 100);")
    self.widget.setObjectName("widget")
    self.dashboardButton = QtWidgets.QPushButton(self.widget)
    self.dashboardButton.setGeometry(QtCore.QRect(0, 250, 201, 51))
    self.dashboardButton.setStyleSheet("QPushButton {\n"
    "    background-color: rgb(255, 255, 255);\n"
    "    color: #FFFFFF;\n"
    "    \n"
    "    padding: 2px;\n"
    "    font: bold 20px; color:black;\n"
    "    \n"
    "    \n"
    "   }\n"
    "QPushButton:hover {\n"
    "    background-color: rgb(98, 98, 98);\n"
    "}")
    self.dashboardButton.setObjectName("dashboardButton")
    self.dashboardButton_2 = QtWidgets.QPushButton(self.widget)
    self.dashboardButton_2.setGeometry(QtCore.QRect(0, 370, 201, 51))
    self.dashboardButton_2.setStyleSheet("QPushButton {\n"
    "    background-color: rgb(255, 255, 255);\n"
    "    color: #FFFFFF;\n"
    "    \n"
    "    padding: 2px;\n"
    "    font: bold 20px; color:black;\n"
    "    \n"
    "    \n"
    "   }\n"
    "QPushButton:hover {\n"
    "    background-color: rgb(98, 98, 98);\n"
    "}")
    self.dashboardButton_2.setObjectName("dashboardButton_2")
    self.label = QtWidgets.QLabel(Dialog)
    self.label.setGeometry(QtCore.QRect(250, 20, 171, 91))
    self.label.setStyleSheet("font-size: 30px;\n"
    "font: bold;\n"
    "")
    self.label.setObjectName("label")
    self.label_2 = QtWidgets.QLabel(Dialog)
    self.label_2.setGeometry(QtCore.QRect(630, 70, 416, 416))
    self.label_2.setText("")
    self.label_2.setPixmap(QtGui.QPixmap("D:/Downloads/numplate.jpg"))
    self.label_2.setScaledContents(True)
    self.label_2.setObjectName("label_2")
    self.label_3 = QtWidgets.QLabel(Dialog)
    self.label_3.setGeometry(QtCore.QRect(640, 530, 401, 241))
    self.label_3.setText("")
    self.label_3.setPixmap(QtGui.QPixmap("D:/Downloads/20886263.jpg"))
    self.label_3.setScaledContents(True)
    self.label_3.setObjectName("label_3")
    self.lineEdit = QtWidgets.QLineEdit(Dialog)
    #self.lineEdit.setText("YO")
    # if string!=None:
    #         print("called")
    #         print(string)
    #         self.lineEdit.setText(string)
    self.lineEdit.setGeometry(QtCore.QRect(300, 280, 221, 41))
    self.lineEdit.setObjectName("lineEdit")
    
    self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
    self.lineEdit_2.setGeometry(QtCore.QRect(300, 360, 221, 41))
    self.lineEdit_2.setObjectName("lineEdit_2")
    self.label_4 = QtWidgets.QLabel(Dialog)
    self.label_4.setGeometry(QtCore.QRect(300, 260, 81, 16))
    self.label_4.setObjectName("label_4")
    self.label_5 = QtWidgets.QLabel(Dialog)
    self.label_5.setGeometry(QtCore.QRect(300, 340, 81, 16))
    self.label_5.setObjectName("label_5")
    self.pushButton = QtWidgets.QPushButton(Dialog)
    self.pushButton.setGeometry(QtCore.QRect(420, 430, 101, 31))
    self.pushButton.setObjectName("pushButton")
    self.pushButton_AI = QtWidgets.QPushButton(Dialog)
    self.pushButton_AI.setGeometry(QtCore.QRect(300, 430, 101, 31))
    self.pushButton_AI.setObjectName("pushButton_AI")

    print("car:"+var)
    self.pushButton.clicked.connect(self.clickme)
    self.pushButton_AI.clicked.connect(self.AI_click)
    lineEdit= self.lineEdit
    self.retranslateUi(Dialog)
    QtCore.QMetaObject.connectSlotsByName(Dialog)
def AI_click(self):
        #self.lineEdit.setText("YO")
        os.system('python detect_video.py --weights ./checkpoints/custom-416 --size 416 --model yolov4 --video 0 --plate')
        #self.lineEdit.setText(line1)


def clickme(self):
    np=self.lineEdit.text()
    cnic=self.lineEdit_2.text()
    table_data(np,cnic)
    
    # printing pressed
    print(np+cnic)
def retranslateUi(self, Dialog):
    _translate = QtCore.QCoreApplication.translate
    Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
    self.dashboardButton.setText(_translate("Dialog", "Dashboard"))
    self.dashboardButton_2.setText(_translate("Dialog", "Record"))
    self.label.setText(_translate("Dialog", "Dashboard"))
    self.label_4.setText(_translate("Dialog", "Number Plate:"))
    self.label_5.setText(_translate("Dialog", "CNIC:"))
    self.pushButton.setText(_translate("Dialog", "Save "))
    self.pushButton_AI.setText(_translate("Dialog", "Auto"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
var=""
ui.setupUi(Dialog,var)
Dialog.show()
sys.exit(app.exec_())
  • Where is line1 defined? – Matt Clarke Aug 29 '22 at 13:34
  • I was experimenting something. I declared line1 as global variable but couldn;t get what I need – Python_bot_here Aug 29 '22 at 13:40
  • If you uncomment `self.lineEdit.setText("YO")` and comment out the rest of `AI_click` does the text change when you click the button? – Matt Clarke Aug 29 '22 at 13:46
  • yes @MattClarke. YO is written in textbox when button is click. But I want string of another py file `os.system('python detect_video.py --weights ./checkpoints/custom-416 --size 416 --model yolov4 --video 0 --plate')` I can't access another file string. If I access another py file String. I'll store in line1 and then run `self.lineEdit.setText(line1)`. But idk how can I do this – Python_bot_here Aug 29 '22 at 15:01
  • 1
    1. Don't use globals; 2. Don't edit pyuic files, but follow the official guidelines about [using Designer](//www.riverbankcomputing.com/static/Docs/PyQt5/designer.html); 3. [`os.system()`](https://docs.python.org/3/library/os.html#os.system) is blocking (which is wrong, as it completely freeze the program until it returns), and doesn't provide any access to the process output, so you should either use the subprocess module (or the QProcess API) or implement whatever `detect_video` does in your script. – musicamante Aug 29 '22 at 15:13
  • Thank you @musicamante I never use these modules. I'll learn these and implement it – Python_bot_here Aug 29 '22 at 15:33
  • @musicamante hello sir I achieved my goal but I got another problem. `detect_video` gives opencv video window so how can I embed video window in PYQT GUI using subprocess. I achieved to store output in variable and setText to display. But I can't find how to attach video window in GUI using Subprocess. – Python_bot_here Aug 29 '22 at 18:15
  • 1
    @Python_bot_here That would be another question (so, it's off topic for this post). In any case, while it's possible to embed external windows in Qt widgets, it's normally discouraged, and an *in-app* solution should be preferred. As already said above, you should consider embedding the code of`detect_video` in your program, so that you can also work with the cv stream instead of using its output. – musicamante Aug 29 '22 at 18:27

1 Answers1

0

Thanks to @musicamante Instead of using os.system('python detect_video.py --weights ./checkpoints/custom-416 --size 416 --model yolov4 --video 0 --plate') use this

def AI_click(self):
        
        s=subprocess.Popen(['python', 'detect_video.py' ,'--weights' 
        ,'./checkpoints/custom-416' ,'--size', '416' ,
        '--model', 'yolov4' ,'--video', '0' ,'--plate'], stdout=subprocess.PIPE)
        output = s.stdout.read()
        s=output.decode()
        self.lineEdit.setText(s)

In other .py file there should be print function