![](/img/trans.png)
[英]Python - pyqt5 - Set text to qtextbrowser with different colors
[英]Set text in PYQT5 python
我的程序是基於人工智能的。 我已經看到有關 Pyqt 在 stackoverflow 中設置文本的所有帖子。 單擊自動(AI)按鈕時,我想在 lineEdit 中設置文本。
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)
基本上 AI 按鈕執行 py 文件並打印字符串。 如何在這個 GUI py 文件中使用 another.py 字符串。 當我的程序執行 py 文件時,它不再記得self.lineEdit.setText(line1)
和字符串。
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_())
感謝@musicamante 而不是使用os.system('python detect_video.py --weights./checkpoints/custom-416 --size 416 --model yolov4 --video 0 --plate')
使用這個
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)
在 other.py 文件中應該有打印 function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.