вывод команды на указанную вкладку PyQt - PullRequest
0 голосов
/ 28 марта 2020

Я использую PyQt5 Python, и я не могу указать вкладку вывода консоли
, мой текущий код дублирует вывод на обеих вкладках в textedit, однако это самое близкое достижение что я хочу. Я попытался использовать pushButton.isEnabled (), но, похоже, он плохо работает с моей установкой, я также попробовал условный оператор в разделе name , но не смог сослаться на действие кнопки. Если у вас есть какие-либо советы о том, как двигаться вперед, пожалуйста, дайте мне знать. Просто чтобы быть понятным, я пытаюсь получить выходной бюджет на вкладке Бюджет и фактические данные на вкладке фактических данных, в настоящее время он печатает на обеих вкладках.

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'DB.ui'
#
# Created by: PyQt5 UI code generator 5.13.2
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox
from subprocess import Popen, PIPE
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QWidget, QApplication
import subprocess
import os
import sys

sys.setrecursionlimit(1500)

class MyStream(QtCore.QObject):
    message = QtCore.pyqtSignal(str)
    def __init__(self, parent=None):
        super(MyStream, self).__init__(parent)

    def write(self, message):
        self.message.emit(str(message))

    #def flush(self):
    #    sys.stdout.flush()


class Ui_Dialog(QtWidgets.QWidget):

    def  __init__(self, parent=None):
        super(Ui_Dialog, self).__init__(parent)

        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)
        self.tabWidget = QtWidgets.QTabWidget(Dialog)
        self.tabWidget.setGeometry(QtCore.QRect(0, 0, 401, 301))
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        self.comboBox = QtWidgets.QComboBox(self.tab)
        self.comboBox.setGeometry(QtCore.QRect(110, 30, 171, 22))
        self.comboBox.setLayoutDirection(QtCore.Qt.RightToLeft)
        self.comboBox.setEditable(True)
        self.comboBox.setCurrentText("Select Fiscal Year")
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("FY14 Budget")
        self.comboBox.addItem("FY15 Budget")
        self.comboBox.addItem("FY16 Budget")
        self.comboBox.addItem("FY17 Budget")
        self.comboBox.addItem("FY18 Budget")
        self.comboBox.addItem("FY19 Budget")
        self.comboBox.addItem("FY19 Budget Upadated")
        self.comboBox.addItem("FY20 Budget")
        self.comboBox.addItem("FY21 Budget")

        self.pushButton = QtWidgets.QPushButton(self.tab)
        self.pushButton.setGeometry(QtCore.QRect(0, 230, 161, 31))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.Budget) 

        self.textEdit = QtWidgets.QTextEdit(self.tab)
        self.textEdit.setGeometry(QtCore.QRect(0, 100, 391, 111))
        self.textEdit.setObjectName("textEdit")


        #self.textEdit.setText(subprocess.check_output((self.Budget)))

        self.pushButton_3 = QtWidgets.QPushButton(self.tab)
        self.pushButton_3.setGeometry(QtCore.QRect(230, 230, 161, 31))
        self.pushButton_3.setObjectName("pushButton_3")

        self.tabWidget.addTab(self.tab, "")
        self.tab_2 = QtWidgets.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.comboBox_2 = QtWidgets.QComboBox(self.tab_2)
        self.comboBox_2.setGeometry(QtCore.QRect(0, 10, 161, 22))
        self.comboBox_2.setEditable(True)
        self.comboBox_2.setObjectName("comboBox_2")
        self.comboBox_2.addItem("July")
        self.comboBox_2.addItem("August")
        self.comboBox_2.addItem("September")
        self.comboBox_2.addItem("October")
        self.comboBox_2.addItem("November")
        self.comboBox_2.addItem("December")
        self.comboBox_2.addItem("January")
        self.comboBox_2.addItem("February")
        self.comboBox_2.addItem("March")
        self.comboBox_2.addItem("April")
        self.comboBox_2.addItem("May")
        self.comboBox_2.addItem("June")

        self.comboBox_3 = QtWidgets.QComboBox(self.tab_2)
        self.comboBox_3.setGeometry(QtCore.QRect(0, 40, 161, 22))
        self.comboBox_3.setEditable(True)
        self.comboBox_3.setObjectName("comboBox_3")
        self.comboBox_3.addItem("2014")
        self.comboBox_3.addItem("2015")
        self.comboBox_3.addItem("2016")
        self.comboBox_3.addItem("2017")
        self.comboBox_3.addItem("2018")
        self.comboBox_3.addItem("2019")
        self.comboBox_3.addItem("2020")
        self.comboBox_3.addItem("2021")

        self.textEdit_2 = QtWidgets.QTextEdit(self.tab_2)
        self.textEdit_2.setGeometry(QtCore.QRect(0, 100, 391, 111))
        self.textEdit_2.setObjectName("textEdit_2")
        self.pushButton_2 = QtWidgets.QPushButton(self.tab_2)
        self.pushButton_2.setGeometry(QtCore.QRect(0, 230, 161, 31))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_2.clicked.connect(self.Actuals) 

        self.pushButton_4 = QtWidgets.QPushButton(self.tab_2)
        self.pushButton_4.setGeometry(QtCore.QRect(230, 230, 161, 31))
        self.pushButton_4.setObjectName("pushButton_4")
        self.tabWidget.addTab(self.tab_2, "")

        self.retranslateUi(Dialog)
        self.tabWidget.setCurrentIndex(0)
        self.comboBox.setCurrentIndex(-1)
        self.comboBox_2.setCurrentIndex(-1)
        self.comboBox_3.setCurrentIndex(-1)
        QtCore.QMetaObject.connectSlotsByName(Dialog)


    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.comboBox.setCurrentText(_translate("Dialog", "Select Fiscal Year"))
        self.pushButton.setText(_translate("Dialog", "Run"))
        self.pushButton_3.setText(_translate("Dialog", "Cancel"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Dialog", "Budget Export"))
        self.comboBox_2.setCurrentText(_translate("Dialog", "Select Month"))
        self.comboBox_3.setCurrentText(_translate("Dialog", "Select Year"))
        self.pushButton_2.setText(_translate("Dialog", "Run"))
        self.pushButton_4.setText(_translate("Dialog", "Cancel"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("Dialog", "Actuals Import"))

    def Actuals(self):
        print("Actuals")

        Monthvalue = self.comboBox_2.currentText() 
        Yearvalue= self.comboBox_3.currentText() 

        Monthdict={
            "July" :"07",
            "August" :"08",
            "September" :"09",
            "October" :"10",
            "November" :"11",
            "December" :"12",
            "January" :"01",
            "February" : "02",
            "March" : "03",
            "April" : "04",
            "May" : "05",
            "June" : "06"}

        #print("Button2: ",pushButton_2)
        concted=str(Monthdict[Monthvalue]) +" & "+ str(Yearvalue)
        print("Actuals: ",concted)

        #cmd=subprocess.Popen([r'newHello.exe',concted],stdout=PIPE, stderr=PIPE,shell=True)
        """
        cmd = [r'BudgetExport.exe',concted]
        #cmd = [r'BudgetExport.py']    
        #stdout, stderr = cmd.communicate()

        output = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
        output.wait(1000)
        #subprocess.check_call('true',shell=True)
        comms=output.communicate()
        print("=====================================")
        msg=comms[0].decode("utf-8")

        print("Output_Type: ",type(msg))
        msg_splt=[]
        msg_splt=msg.split("\r\n")

        print("=====================================")
        #print (comms)
        num=0
        for x in msg_splt:
            num+=1
            print(num,": ",x)

        print("=====================================")
        #print (comms)
        """

    def Budget(self):
        value = self.comboBox.currentText() 

        Datedict={
          "FY14 Budget" :"07/2013",
          "FY15 Budget" :"07/2014",
          "FY16 Budget" :"07/2015",
          "FY17 Budget" :"07/2016",
          "FY18 Budget" :"07/2017",
          "FY19 Budget" :"07/2018",
          "FY19 Budget Upadated" :"07/2018",
          "FY20 Budget" : "07/2019",
          "FY21 Budget" : "07/2020"}

        concted=str(value) +" & "+ str(Datedict[value])
        #cmd=subprocess.Popen([r'newHello.exe',concted],stdout

        cmd = [r'BudgetExport.exe',concted]
        #cmd = [r'BudgetExport.py']    
         #stdout, stderr = cmd.communicate()

        output = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
        output.wait(1000)
        #subprocess.check_call('true',shell=True)
        comms=output.communicate()
        print("=====================================")
        msg=comms[0].decode("utf-8")

        print("Output_Type: ",type(msg))
        msg_splt=[]
        msg_splt=msg.split("\r\n")

        print("=====================================")
    #print (comms)
        num=0
        for x in msg_splt:
           num+=1
           print(num,": ",x)

        print("=====================================")

    @QtCore.pyqtSlot(str)
    def Budget_myStream_message(self, message):
         self.textEdit.moveCursor(QtGui.QTextCursor.End)
         self.textEdit.insertPlainText(message)

    @QtCore.pyqtSlot(str)
    def Actual_myStream_message(self, message):
         self.textEdit_2.moveCursor(QtGui.QTextCursor.End)
         self.textEdit_2.insertPlainText(message)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()

    ui = Ui_Dialog()
    ui.__init__(Dialog)
    Dialog.show()
    myStream = MyStream()

    myStream.message.connect(ui.Budget_myStream_message)  
    myStream.message.connect(ui.Actual_myStream_message)



    sys.stdout = myStream
    sys.exit(app.exec_())

1 Ответ

0 голосов
/ 28 марта 2020

Я думаю, что подключение / отключение функции записи на лету сработало бы.

Ваш код довольно длинный, поэтому здесь приведены только те части, которые я изменил:

class Ui_Dialog(QtWidgets.QWidget):

    def  __init__(self, parent=None):
        super(Ui_Dialog, self).__init__(parent)
        ...
        sys.stdout = MyStream()
        sys.stdout.message.connect(self.Budget_myStream_message)
        sys.stdout.message.connect(self.Actual_myStream_message)
        ...

    def Actuals(self):
        sys.stdout.message.disconnect(self.Budget_myStream_message)
        sys.stdout.message.connect(self.Actual_myStream_message)
        ...
        print("Actuals")
        ...

    def Budget(self):
        sys.stdout.message.connect(self.Budget_myStream_message)
        sys.stdout.message.disconnect(self.Actual_myStream_message)
        ...
        print("Budget")
        ...


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()

    ui = Ui_Dialog()
    ui.__init__(Dialog)
    Dialog.show()
    # Delete that:
    #myStream = MyStream()
    #myStream.message.connect(ui.Budget_myStream_message)  
    #myStream.message.connect(ui.Actual_myStream_message)
    sys.stdout = myStream
    sys.exit(app.exec_())
...