I copied and saved dummy_script.py
and ran it with app provided in the section " Getting data from the QProcess
". When I run it and click “execute”, I only get 3 lines of text:
Executing process
State changed: Starting
State changed: Not running
When in the example there are also things like “total complete: 50%” etc. In other words, nothing that’s in dummy_script.py
gets printed out by the app. I didn’t change anything in the code, I’m running it as it’s provided. I’m using python 3.12.7 on Windows 10.
@krzysiek_scarface can you provide listing number? I can’t figure out which code you meant.
Every code block in book has own unique number.
I don’t have a book, I am following tutorial on the website. This part: Run external programs in PySide6 with QProcess, with streams and progress bars
There are two pieces of code. First, “dummy_script”:
import sys
import time
def flush_then_wait():
sys.stdout.flush()
sys.stderr.flush()
time.sleep(0.5)
sys.stdout.write("Script stdout 1\n")
sys.stdout.write("Script stdout 2\n")
sys.stdout.write("Script stdout 3\n")
sys.stderr.write("Total time: 00:05:00\n")
sys.stderr.write("Total complete: 10%\n")
flush_then_wait()
sys.stdout.write("name=Martin\n")
sys.stdout.write("Script stdout 4\n")
sys.stdout.write("Script stdout 5\n")
sys.stderr.write("Total complete: 30%\n")
flush_then_wait()
sys.stderr.write("Elapsed time: 00:00:10\n")
sys.stderr.write("Elapsed time: 00:00:50\n")
sys.stderr.write("Total complete: 50%\n")
sys.stdout.write("country=Nederland\n")
flush_then_wait()
sys.stderr.write("Elapsed time: 00:01:10\n")
sys.stderr.write("Total complete: 100%\n")
sys.stdout.write("Script stdout 6\n")
sys.stdout.write("Script stdout 7\n")
sys.stdout.write("website=www.mfitzp.com\n")
flush_then_wait()
Second:
from PySide6.QtWidgets import (QApplication, QMainWindow, QPushButton, QPlainTextEdit,
QVBoxLayout, QWidget)
from PySide6.QtCore import QProcess
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.p = None
self.btn = QPushButton("Execute")
self.btn.pressed.connect(self.start_process)
self.text = QPlainTextEdit()
self.text.setReadOnly(True)
l = QVBoxLayout()
l.addWidget(self.btn)
l.addWidget(self.text)
w = QWidget()
w.setLayout(l)
self.setCentralWidget(w)
def message(self, s):
self.text.appendPlainText(s)
def start_process(self):
if self.p is None: # No process running.
self.message("Executing process")
self.p = QProcess() # Keep a reference to the QProcess (e.g. on self) while it's running.
self.p.readyReadStandardOutput.connect(self.handle_stdout)
self.p.readyReadStandardError.connect(self.handle_stderr)
self.p.stateChanged.connect(self.handle_state)
self.p.finished.connect(self.process_finished) # Clean up once complete.
self.p.start("python3", ['dummy_script.py'])
def handle_stderr(self):
data = self.p.readAllStandardError()
stderr = bytes(data).decode("utf8")
self.message(stderr)
def handle_stdout(self):
data = self.p.readAllStandardOutput()
stdout = bytes(data).decode("utf8")
self.message(stdout)
def handle_state(self, state):
states = {
QProcess.NotRunning: 'Not running',
QProcess.Starting: 'Starting',
QProcess.Running: 'Running',
}
state_name = states[state]
self.message(f"State changed: {state_name}")
def process_finished(self):
self.message("Process finished.")
self.p = None
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec()
I’m guessing there is some small piece missing, as something like that happened before.
try change line to:
...
self.p.start("python", ['dummy_script.py'])
...
btw… this example is working in my end
I think that topic on Stack Overflow explains it well.
“As of April 2023 (and possibly earlier), the official python installer only provides/creates python.exe
and while it adds this to the %PATH%
for ease-of-reference it does NOT create python3.exe
, so any scripts or code with commands like python3 setup.py
would need to be converted to python setup.py
”