2017-02-16 25 views
0

就PyQT的使用經驗而言,我遵循示例代碼HERE來做到這一點,但我對如何將啓動下載部分與啓動GUI部件,以便我可以在按下OK(startBtn)按鈕時啓動該部件。另外,知道我所做的命令不會做任何事情,但給你一個錯誤,但我知道這是有效的。
任何幫助表示讚賞!PyQt5:想通過點擊按鈕啓動一個特定的子進程

from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QAction, qApp, QDesktopWidget, QPushButton, QHBoxLayout, QVBoxLayout, QTextEdit 
from PyQt5.QtGui import QIcon 
from PyQt5.QtCore import QThread, QProcess 
import sys 


class GUI(QProcess): 
    def __init__(self): 
     super().__init__() 

     # Create an instance variable here (of type QTextEdit) 
     startBtn = QPushButton('OK') 
     stopBtn = QPushButton('Cancel') 

     #startBtn.clicked.connect() 
     stopBtn.clicked.connect(qApp.exit) 

     self.hbox = QHBoxLayout() 
     self.hbox.addStretch(1) 
     self.hbox.addWidget(startBtn) 
     self.hbox.addWidget(stopBtn) 
     self.edit = QTextEdit() 

     self.edit.setWindowTitle("QTextEdit Standard Output Redirection") 

     self.vbox = QVBoxLayout() 
     self.vbox.addStretch(1) 
     self.vbox.addWidget(self.edit) 
     self.vbox.addLayout(self.hbox) 

     #setLayout(self.vbox) 
     self.central=QWidget() 

     #self.vbox.addWidget(self.edit) 
     self.central.setLayout(self.vbox) 
     self.central.show() 

    def readStdOutput(self): 
     self.edit.append(str(self.readAllStandardOutput())) 




def main(): 
    app = QApplication(sys.argv) 
    qProcess = GUI() 

    qProcess.setProcessChannelMode(QProcess.MergedChannels); 
    qProcess.start("youtube-dl") 
    qProcess.readyReadStandardOutput.connect(qProcess.readStdOutput); 

    return app.exec_() 


if __name__ == '__main__': 
    main() 

2注:

  1. 如果您還知道如何當你按下它,直到處理完畢,那麼我很想知道禁用OK按鈕。

  2. 並非所有的進口都被使用,但我可以在以後清理。 PyCharm顯示哪個被使用而不是。清理是爲了以後。

回答

1

要做到你的要求,你必須有一些注意事項:

  • youtube-dl需要的參數,如URL,爲了這個,我已經把一個QLineEdit

  • 要知道什麼時候該進程開始和結束,我們使用的信號:stateChanged(newState)

完整代碼:

import sys 

from PyQt5.QtCore import QProcess 
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QVBoxLayout, QTextEdit, QLabel, QLineEdit 


class GUI(QProcess): 
    def __init__(self, parent=None): 
     super(GUI, self).__init__(parent=parent) 

     # Create an instance variable here (of type QTextEdit) 
     self.startBtn = QPushButton('OK') 
     self.stopBtn = QPushButton('Cancel') 

     self.hbox = QHBoxLayout() 
     self.hbox.addStretch(1) 
     self.hbox.addWidget(self.startBtn) 
     self.hbox.addWidget(self.stopBtn) 

     self.label = QLabel("Url: ") 
     self.lineEdit = QLineEdit() 

     self.lineEdit.textChanged.connect(self.EnableStart) 

     self.hbox2 = QHBoxLayout() 
     self.hbox2.addWidget(self.label) 
     self.hbox2.addWidget(self.lineEdit) 

     self.edit = QTextEdit() 
     self.edit.setWindowTitle("QTextEdit Standard Output Redirection") 

     self.vbox = QVBoxLayout() 
     self.vbox.addStretch(1) 

     self.vbox.addLayout(self.hbox2) 
     self.vbox.addWidget(self.edit) 
     self.vbox.addLayout(self.hbox) 

     self.central = QWidget() 

     self.central.setLayout(self.vbox) 
     self.central.show() 

     self.startBtn.clicked.connect(self.startDownload) 
     self.stopBtn.clicked.connect(self.kill) 
     self.stateChanged.connect(self.slotChanged) 

     self.EnableStart() 

    def slotChanged(self, newState): 
     if newState == QProcess.NotRunning: 
      self.startBtn.setDisabled(False) 
     elif newState == QProcess.Running: 
      self.startBtn.setDisabled(True) 

    def startDownload(self): 
     self.start("youtube-dl", [self.lineEdit.text()]) 

    def readStdOutput(self): 
     self.edit.append(str(self.readAllStandardOutput())) 

    def EnableStart(self): 
     self.startBtn.setDisabled(self.lineEdit.text() == "") 


def main(): 
    app = QApplication(sys.argv) 
    qProcess = GUI() 

    qProcess.setProcessChannelMode(QProcess.MergedChannels) 
    qProcess.readyReadStandardOutput.connect(qProcess.readStdOutput) 

    return app.exec_() 


if __name__ == '__main__': 
    main() 

截圖:

enter image description here

+0

臨屋恩,這會幫助你一噸!這對我來說很難。我使用Youtube-dl和手動更新的.txt文件,以及其他一些參數,但我沒有提供。現在我將學習如何使用GUI更改/添加參數。 – Thomasedv

+0

如果我的答案可以幫助您將其標記爲正確。 – eyllanesc

+0

當然可以。剛剛谷歌如何做到這一點... - .-我是新來的網站...這是如此明顯... – Thomasedv