Executing a PyQt5 GUI which includes subprocesses from the Linux terminal causes a black screen in the GUI and freezes it
You should not use Popen since the communicate()
method is blocking, instead use QProcess
:
#!/usr/bin/python3.5
from PyQt5 import QtCore, QtWidgets
class GUI(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.initGUI()
self.behaviours()
def initGUI(self):
self.button = QtWidgets.QPushButton("Button")
self.label = QtWidgets.QLabel()
box = QtWidgets.QVBoxLayout(self)
box.addWidget(self.button)
box.addWidget(self.label)
self.show()
def behaviours(self):
self._onedrive_process = QtCore.QProcess(self)
self._onedrive_process.setProcessChannelMode(QtCore.QProcess.MergedChannels)
self._onedrive_process.readyReadStandardOutput.connect(
self.on_readyReadStandardOutput
)
self._onedrive_process.setProgram("onedrive")
self.button.clicked.connect(self.connect_to_onedrive)
@QtCore.pyqtSlot()
def connect_to_onedrive(self):
self._onedrive_process.start()
@QtCore.pyqtSlot()
def on_readyReadStandardOutput(self):
result = self._onedrive_process.readAllStandardOutput()
print(result)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ex = GUI()
sys.exit(app.exec_())
Update:
If you want to pass options to the command you must use setArguments()
:
from PyQt5 import QtCore, QtGui, QtWidgets
class OneDriveManager(QtCore.QObject):
logChanged = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self._process = QtCore.QProcess(self)
self._process.readyReadStandardOutput.connect(self.on_readyReadStandardOutput)
self._process.setProgram("onedrive")
def launch(self, options=None):
self._process.setArguments(options)
if self._process.state() != QtCore.QProcess.NotRunning:
self._process.kill()
self._process.start()
def help(self):
self.launch(["--help"])
def synchronize(self):
self.launch(["--synchronize"])
@QtCore.pyqtSlot()
def on_readyReadStandardOutput(self):
res = self._process.readAllStandardOutput().data().decode()
self.logChanged.emit(res)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self._onedrive_manager = OneDriveManager(self)
help_button = QtWidgets.QPushButton("Help")
help_button.clicked.connect(self._onedrive_manager.help)
synchronize_button = QtWidgets.QPushButton("Synchronize")
synchronize_button.clicked.connect(self._onedrive_manager.synchronize)
log_plaintextedit = QtWidgets.QPlainTextEdit()
self._onedrive_manager.logChanged.connect(log_plaintextedit.setPlainText)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(help_button)
lay.addWidget(synchronize_button)
lay.addWidget(log_plaintextedit)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
How to get exe stdout in real time in GUI though cmd is executing in Python?
QtCore.QProcess
has readyReadStandardError
and readyReadStandardOutput
signals which emited once data on stdout and stderr ready
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import Qt
def buffer_to_str(buf):
codec = QtCore.QTextCodec.codecForName("UTF-8")
return str(codec.toUnicode(buf))
class Process(QtCore.QObject):
stdout = QtCore.pyqtSignal(str)
stderr = QtCore.pyqtSignal(str)
finished = QtCore.pyqtSignal(int)
def start(self, program, args):
process = QtCore.QProcess()
process.setProgram(program)
process.setArguments(args)
process.readyReadStandardError.connect(lambda: self.stderr.emit(buffer_to_str(process.readAllStandardError())))
process.readyReadStandardOutput.connect(lambda: self.stderr.emit(buffer_to_str(process.readAllStandardOutput())))
process.finished.connect(self.finished)
process.start()
self._process = process
if __name__ == "__main__":
app = QtWidgets.QApplication([])
process = Process()
log = QtWidgets.QPlainTextEdit()
log.show()
process.stderr.connect(log.appendPlainText)
cmd = "ping"
args = ["google.com"]
process.start(cmd, args)
app.exec_()
CI: My_Controller not found when uploaded to webhost
This is indeed a problem with case sensitivity, just like what zourite said. The problem here is that, you have to name the file MY_Controller.php
and the class MY_Controller
. Notice the uppercase "C".
Minecraft Server Executing Scripts
You can create a new plugin for Bukkit/Spigot (more information here).
In the onCommand
-Method you can then call Runtime.getRuntime().exec("your shell command")
to run commands in the linux shell (can also be used on Windows servers).
See also the Java documentation.
How do I copy a string to the clipboard?
Actually, pywin32
and ctypes
seem to be an overkill for this simple task. Tkinter
is a cross-platform GUI framework, which ships with Python by default and has clipboard accessing methods along with other cool stuff.
If all you need is to put some text to system clipboard, this will do it:
from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('i can has clipboardz?')
r.update() # now it stays on the clipboard after the window is closed
r.destroy()
And that's all, no need to mess around with platform-specific third-party libraries.
If you are using Python 3, replace TKinter
with tkinter
.
Why does pip install inside Python raise a SyntaxError?
pip is run from the command line, not the Python interpreter. It is a program that installs modules, so you can use them from Python. Once you have installed the module, then you can open the Python shell and do import selenium
.
The Python shell is not a command line, it is an interactive interpreter. You type Python code into it, not commands.
Related Topics
To Read Line from File Without Getting "\N" Appended at the End
Are Python Variables Pointers? or Else, What Are They
What Does the Slash Mean in Help() Output
Python Error "Importerror: No Module Named"
Best Way to Replace Multiple Characters in a String
Python: List VS Dict for Look Up Table
How to Parse Dates with -0400 Timezone String in Python
How to Rotate an Image Around an Off Center Pivot in Pygame
How to Direct Output to a File When There Are Utf-8 Characters
Scheduling a Regular Event: Cron/Cron Alternatives (Including Celery)
Select Iframe Using Python + Selenium
Sorting a Python List by Two Fields
How to Find the Time Difference Between Two Datetime Objects in Python