python - PyQt5: Want to start a specific subprocess with a button click -
one day old in terms of experience pyqt, followed sample code here this, clueless how separate start download part start gui part, can instead start when press ok (startbtn)button. also, know command doesn't give error, know works.
appreciated!
from pyqt5.qtwidgets import qapplication, qwidget, qmainwindow, qaction, qapp, qdesktopwidget, qpushbutton, qhboxlayout, qvboxlayout, qtextedit pyqt5.qtgui import qicon pyqt5.qtcore import qthread, qprocess import sys class gui(qprocess): def __init__(self): super().__init__() # create 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 notes:
if know how disable ok button when press it, until process finished, i'd love know.
not imports used, can clean later. pycharm show used , not. cleanup later.
to ask have have considerations:
youtube-dl
requires parameters, url, have placedqlineedit
.to know when process starts , ends, use signal:
statechanged(newstate)
complete code:
import sys pyqt5.qtcore import qprocess 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 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()
screenshot:
Comments
Post a Comment