GUI interaction with terminal - python

Is it possible to set up a Python script with a PyQt5 GUI such that an active terminal can interact with it?
An example project:
from PyQt5 import QtWidgets from PyQt5.QtCore import pyqtSlot
class Machine:
"""Simpel state machine"""
STATE_IDLE = 'idle'
STATE_OPERATION = 'operation'
def __init__(self):
self.state = self.STATE_IDLE
def get_state(self):
return self.state
def set_state(self, new_state):
self.state = new_state
#pyqtSlot() def on_click():
"""Callback for the PyQt button"""
print('State:', machine.get_state())
if __name__ == '__main__':
# Create machine instance
machine = Machine()
# Set up GUI
app = QtWidgets.QApplication([])
window = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
label = QtWidgets.QLabel('Hello World!')
button = QtWidgets.QPushButton('Go')
button.clicked.connect(on_click)
layout.addWidget(label)
layout.addWidget(button)
window.setLayout(layout)
window.show()
code = app.exec()
exit(code)
I would like to run this script and to then be able to access the objects from the same terminal. Something like:
$ python main.py
State: idle [< I just pressed the GUI button]
>> machine.set_state(Machine.STATE_OPERATION)
State: operation [< I just pressed the GUI button again]
If I run it like:
python -i main.py
The command line hangs until the application was closed (that's what .exec() does after all), but then I can access machine. Can I make .exec() not hang like this? Maybe run app from a separate thread?
EDIT: I found that thy Pyzo IDE allows you to create a shell with PyQt5 support, which handles the PyQt event loop. So when using the script above with such a Pyzo shell, I can remove code = app.exec() exit(code) because Pyzo will run the event loop and keep the window alive. Now I'd like to do the same across all IDEs.

Related

Restarting Kernel whenever i exit the gui without error message

Currently trying to build a GUI.
Whenever I try to exit the GUI using either the button I created or the exit button on the OS it takes some time and at the end the console shows Restarting kernel... without any other msg.
I am using Spyder with Python 3.9.
BTW I am not using sys.exit() as I saw in some other questions that it was the problem kind of.. I really don't get it..
The code :
class Class_Thread(filex.Classx,QThread):
def run(self):
self.setTerminationEnabled(True)
"""
the code the gui runs
"""
class UI(QMainWindow):
def __init__(self):
super(UI,self).__init__()
#load the UI file
uic.loadUi("test.ui", self)
self.Class_instance = Class_Thread()
#define the widgets
self.start_button = self.findChild(QPushButton,"start_button")
self.reset_button = self.findChild(QPushButton,"reset")
self.exit_button = self.findChild(QPushButton,"exit")
self.start_button.clicked.connect(self.start_check)
self.reset_button.clicked.connect(self.reset_code)
self.exit_button.clicked.connect(QtCore.QCoreApplication.instance().quit)
#show the app
self.show()
def reset_code(self):
self.Class_instance.quit()
self.reset_button.setEnabled(False)
self.start_button.setEnabled(True)
def start_check(self):
self.reset_button.setEnabled(True)
self.start_button.setEnabled(False)
self.CC_Instance.start()
app = QApplication(sys.argv)
UIWindow = UI()
UIWindow.show()
UIWindow.activateWindow()
app.exec_()

Launch Python Script with Pythonw from Batch and give it Focus

I'm on Windows 10 and I have a PyQt5 Application I launch using a .bat file to use the venv interpreter.
When I call the script using python my_script.py it opens the main window in focus, but also shows the Python console in background. To get rid of the console, I tried launching it with pythonw my_script.py, but then it silently opens in background.
I tried things like window.setWindowState(Qt.WindowState.WindowActive) or window.setFocus(), but this only makes the icon blink in the task bar. Other Google results said that Windows does not allow programs to easily grab focus anymore, but then again, python can do it on start-up, so I would like to replicate that behavior with pythonw.
Below you can find testing code and the batch file, context was launching it from a custom URI protocol.
# https://stackoverflow.com/a/38205984 to register any protocol for testing
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self, title):
super().__init__()
self.setWindowTitle("Test App")
label = QLabel(title)
self.setCentralWidget(label)
if __name__ == '__main__':
if len(sys.argv) == 1:
the_title = "I got no arguments"
else:
the_title = f"I was run with argument {sys.argv[1]}"
app = QApplication(sys.argv)
window = MainWindow(the_title)
window.show()
window.setFocus()
app.exec()
and
cd %~dp0
call ..\venv\Scripts\activate
start "" "pythonw" "test_url_scheme_one.py" "%1"
deactivate
Replacing the batch file with the following fixed it:
start "" "C:\path\to\venv\Scripts\pythonw.exe" "C:\path\to\my_script.py" "%1"
Now the window gets started in foreground and in focus.

Launch pyqt dialog window multiple times

I've put together some test code to figure out how to launch a simple pyqt Ui_Dialog multiple times during a script. I want launch the window depending on certain conditions as my script is running. The idea is that it is just a popup notification window with different text and the user will click ok. I designed the window in QtDesigner and inherited the generated code in another class:
class MsgWindow(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.app = QtGui.QApplication(sys.argv)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.OK, QtCore.SIGNAL('clicked()'),self.close)
def AddText(self,text):
""" Method to add text to textEdit box """
self.ui.textEdit.append(text + '\n')
def closeEvent(self, event):
print "Closing the app"
#self.deleteLater()
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
myapp = MsgWindow()
myapp.show()
sys.exit(app.exec_())
So then my test script just launches the window (user closes it) and repeats. The issue is that after the last close, python crashes (python.exe has stopped working). Here is the test script code:
app = QtGui.QApplication(sys.argv)
dlg = MsgWindow()
for x in range(0,3):
dlg.AddText("This is a test, iteration: %d"%x)
dlg.exec_()
#sys.exit(app.exec_())
time.sleep(2)
What is the correct way to do this without crashing?
Thank you.

Pyqt Terminal hangs after excuting close window command

I have read lots of threads online, but still I could not find the solution. My question should be very simple: how to close a Pyqt window WITHOUT clicking a button or using a timer.
The code I tried is pasted below
from PyQt4 import QtGui, QtCore
import numpy as np
import progressMeter_simple
import sys
import time
import pdb
class ProgressMeter(progressMeter_simple.Ui_Dialog, QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
progressMeter_simple.Ui_Dialog.__init__(self)
self.setupUi(self)
self.progressBar.setRange(0, 0)
QtGui.QApplication.processEvents()
def termination(self):
time.sleep(5)
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
Dialog = ProgressMeter()
Dialog.show()
Dialog.termination()
sys.exit(app.exec_())
My Pyqt GUI is designed using Qt designer, and it is nothing but a progress bar that keeps moving from left to right (busy indication).
However, when I run the code above, the terminal still hangs after the Pyqt window is closed. Ctrl+C also couldn't kill the process.
In short, how can I properly close/terminate a Pyqt window without clicking a button or using a timer?
It's not working because you're calling GUI methods on the dialog (close()) outside of the event loop. The event loop doesn't start until you call app.exec_().
If you really want to close the dialog immediately after it opens without using a QTimer, you can override the showEvent() method and call termination() from there, which gets called when the dialog is first displayed.
class ProgressMeter(progressMeter_simple.Ui_Dialog, QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
progressMeter_simple.Ui_Dialog.__init__(self)
self.setupUi(self)
self.progressBar.setRange(0, 0)
def showEvent(self, event):
super(ProgressMeter, self).showEvent(event)
self.termination()

How to Restart PyQt4 Application

Is there a way to restart PyQt application QApplication
I have an app created with pyqt4 and python 2.6 using below code
app = QtGui.QApplication(sys.argv)
i have settings options where i set some settings. Now when i save settings i need to reload the application so that new settings are effected. Without the need of end user to exit and launch the app again.
I had a similar problem and simply used this at the appropriate place:
subprocess.Popen([__file__])
sys.exit(0)
It was a simple application, and didn't need any further arguments.
I explain how I did it :
I create a extra one file main.py which calls my actual main program file dash.py.
And I emits a signal for restarting (my programs auto updates at the closeEvent) so I required to emit a signal for it. This is the snippets hope this will help you.
This one is in my main program file in dash.py
def restart(self):
# create a signal equivalent to "void someSignal(int, QWidget)"
self.emit(QtCore.SIGNAL("RESTARTREQUIRED"), True)
This one in main.py which calls actual program only and restarts the app
import sys
from PyQt4 import QtGui,QtCore
from bin import dash
if __name__ == "__main__":
application = QtGui.QApplication(sys.argv)
uDesk = dash.app()
uDesk.show()
uDesk.actionRestart.triggered.disconnect()
# define restart slot
#QtCore.pyqtSlot()
def restartSlot():
print 'Restarting app'
global uDesk
uDesk.deleteLater()
uDesk = dash.app()
uDesk.show()
uDesk.actionRestart.triggered.disconnect()
uDesk.actionRestart.triggered.connect(restartSlot)
print 'New app started !'
QtCore.QObject.connect(uDesk,
QtCore.SIGNAL("RESTARTREQUIRED"),
restartSlot)
uDesk.actionRestart.triggered.connect(restartSlot)
sys.exit(application.exec_())
Hope this was helpful !!
EDIT: Changing the way to get the application path
You could just start a new process and exit yours, something like this: (CODE NOT TESTED, but based on this answer)
// Restart Application
def restart(self, abort):
// Spawn a new instance of myApplication:
proc = QProcess()
//proc.start(self.applicationFilePath());
import os
proc.start(os.path.abspath(__file__))
self.exit(0);
Code it as a method of your Qapplication or even a function if you don't feel like subclassing
This is how I restart TicTacToe game in PySide (it should be the same in PyQt):
I have a single class - a QWidget class - in which is coded the Tic Tac Toe game. To restart the application I use:
import subprocess
a QPushButton() like so:
self.button = QPushButton("Restart", self)
the connection of the button to Slot:
self.buton.clicked.connect(self.restartGame)
the Slot for this button, like so:
def restartGame(self):
self.close()
subprocess.call("python" + " TicTAcToe.py", shell=True)
All these are in the same - single - class. And what these do: close the active window of the game and create a new one.
How this code looks in the TicTacToe class:
import subprocess
class TicTacToe(QWidget):
def __init__(self):
QWidget.__init__(self)
self.button = QPushButton("Restart", self)
self.buton.clicked.connect(self.restartGame)
def restartGame(self):
self.close()
subprocess.call("python" + " TicTacToe.py", shell=True)
def main():
app = QApplication(sys.argv)
widget = TicTacToe()
widget.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
EDIT
I know this doesn't answer the question (it doesn't restart a QApplication), but I hope this helps those who want to restart their QWidget single class.

Categories

Resources