I am getting an issue when trying to open a PyQt window.
The code below is an example of my original code. When I imported the module in import Test and ran test.Start(), I got the following error:
QCoreApplication::exec: The event loop is already running
After some research, I found out it was because I had already already made a QApplication.
test.py....
import sys
def Start():
app = QApplication(sys.argv)
m = myWindow()
m.show()
app.exec_()
class myWindow():....
if __name__ == "__main__":
Start()
So then I read that I could rewrite my code like this and it would fix the error:
test.py....
def Start():
m = myWindow()
m.show()
class myWindow():....
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
Start()
app.exec_()
Now I no longer get the QCoreApplication::exec: The event loop is already running error, but my window closes almost immediately after opening.
You need to keep a reference to the opened window, otherwise it goes out of scope and is garbage collected, which will destroy the underlying C++ object also. Try:
def Start():
m = myWindow()
m.show()
return m
class myWindow():....
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = Start()
app.exec_()
You can also do:
def Start():
global m
m = myWindow()
m.show()
class myWindow():....
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = Start()
app.exec_()
Use the following code. Your problem is in your imports and using "show" as a name for function as far as I assume. You haven't provided what you have written in your class, so it's difficult to guess. But following code works like a charm. ;-)
Best wishes, good luck!
import sys
from PyQt5 import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
app = QApplication(sys.argv)
def Start():
m = myWindow()
m.showWid()
sys.exit(app.exec())
class myWindow:
def __init__(self):
self.window = QWidget()
self.window.setWindowTitle("Program Title")
self.window.setFixedWidth(600)
self.window.setStyleSheet("background: #18BEBE;")
def showWid(self):
self.window.show()
if __name__ == "__main__":
Start()
Related
I guess that I am not closing my PyQT5 Window correctly. I am using spyder (3.3.5) which I have installed with anaconda, to program a pyqt5 program. I am using qt creator to design my ui file, which I load using the loadUi function in pyqt package. Everything works fine with the code, until I need to close it. I close the window via the close button (the x button in the top right). The window itself is closed, but it seems like the console (or shell) is stuck, and I can't give it further commands or to re run the program, without having to restart the kernel (to completely close my IDE and re opening it).
I have tried looking for solutions to the problem in the internet, but none seems to work for me. Including changing the IPython Console > Graphics to automatic.
Edit: Also Created an issure:
https://github.com/spyder-ide/spyder/issues/9991
import sys
from PyQt5 import QtWidgets,uic
from PyQt5.QtWidgets import QMainWindow
class Mic_Analysis(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui=uic.loadUi("qt_test.ui",self)
...
if __name__ == "__main__":
def run_app():
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app=QtWidgets.QApplication.instance()
mainWin=Mic_Analysis()
mainWin.show()
app.exec_()
run_app()
If someone have any suggestion I would be very happy to hear them.
For me, it helped to remove the 'app.exec_()' command.
But then it closes immediatly when running the code. To keep the window open, I needed to return the MainWindow instance to the global scope (or make it a global object). My code looks like this:
from PyQt5 import QtWidgets
import sys
def main():
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
main = MainWindow()
main.show()
return main
if __name__ == '__main__':
m = main()
Try adding :
app.setQuitOnLastWindowClosed(True)
to your main() function
def main():
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(True)
win = MainWindow()
win.show()
sys.exit(app.exec_())
Surprizinglly, for me this works well. No QApplication is needed.
It seems to work in another thread.
from PyQt5 import QtWidgets,uic
class Ui(QtWidgets.QDialog):
def __init__(self):
super().__init__()
uic.loadUi('./test.ui',self)
self.show()
w=Ui()
I have used part of a code (PyQt5) from this post
from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication
class Main(QTreeView):
def __init__(self):
QTreeView.__init__(self)
model = QFileSystemModel()
model.setRootPath('C:\\')
self.setModel(model)
self.doubleClicked.connect(self.test)
def test(self, signal):
file_path=self.model().filePath(signal)
print(file_path)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
And i have a problem with this line
model.setRootPath('C:\')
When i run the program it always shows drives like C: D: just not the content of C:\ or even if i type "C:\Users\" or a path that doesn't even exists, it always just shows, see attached image, What am i doing wrong?
Image of PyQt Program showing file manager
I am using:
Windows 10,
PyCharm,
Python 3.5,
PyQt5,
Thanks for your help.
You must indicate to the QTreeView what is your root item with setRootIndex():
from PyQt5.QtCore import QDir
from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication
class Main(QTreeView):
def __init__(self):
QTreeView.__init__(self)
model = QFileSystemModel()
self.setModel(model)
model.setRootPath(QDir.rootPath())
self.setRootIndex(model.index("C:"))
self.doubleClicked.connect(self.test)
def test(self, signal):
file_path=self.model().filePath(signal)
print(file_path)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
I can't input anything from the user in the terminal while running a PyQt Application.
Actually i am creating something but i can't show the whole code here so there is the main of the code, and believe that i need the fix of it only:
from PyQt4.QtGui import *
import sys
def window():
app = QApplication(sys.argv)
window = QWidget()
btn = QPushButton()
btn.setText("Input In Console")
box = QFormLayout()
box.addRow(btn)
btn.clicked.connect(input_txt)
window.setLayout(box)
window.show()
sys.exit(app.exec_())
def input_txt():
input("Enter you Name ")
if __name__ == "__main__":
window()
And while running as i press the button a disaster of loop starts:
I really tried a lot configuring the solution of this problem but all failed. Hope these information helped, if any question regarding the post then please say in comment.
I don't need this anymore but still i am posting answer for if someone else having problem with the same.
Solution : Use Threads
from PyQt4.QtGui import *
import sys,threading
def window():
app = QApplication(sys.argv)
window = QWidget()
btn = QPushButton()
btn.setText("Input In Console")
box = QFormLayout()
box.addRow(btn)
btn.clicked.connect(input_txt)
window.setLayout(box)
window.show()
sys.exit(app.exec_())
def input_txt():
thread = threading.Thread(target=input)
thread.start()
if __name__ == "__main__":
window()
How can I use the QProcess.finished() to call a different Python3 script.
Here's the script I call:
#!/usr/bin/python
from PyQt4.QtGui import QApplication
from childcontrolgui import childcontrolgui
def main():
import sys
app = QApplication(sys.argv)
wnd = childcontrolgui()
wnd.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
To call the script I use the code as seen here
def properties(self):
command="python3 ../GUI/main.py"
self.process=QProcess()
self.process.finished.connect(self.onFinished)
self.process.startDetached(command)
def onFinished(self, exitCode, exitStatus):
self.Check_Timer.stop()
self.Logout_Timer.stop()
self.Firstrun=True
self.initControl()
Starting of the process works, the window from main.py is shown, but it seems, finished isn't fired. Nothing happens, when I close the Window from main.py
You can't get a signal when you use startDetached() because you have no object. Use ordinary start() instead.
And don't forget to start QApplication within control script, too.
class Control(QObject):
def properties(self):
self.process=QProcess()
self.process.finished.connect(self.onFinished)
self.process.start('python3', ['../GUI/main.py'])
def onFinished(self, exitCode, exitStatus):
[...]
if __name__ == '__main__':
app = QApplication(sys.argv)
co = Control()
co.properties()
sys.exit(app.exec_())
I'm Working with PySide, and seeking a way to restart my Qt app. Does this depends on python, or it has to be controlled by Qt?
BY ROSTYSLAV'S SUGGESTION BELLOW :
class MyAppMainWindow(QMainWindow):
def __init__(self):
self.EXIT_CODE_REBOOT = -15123123
exit_code = self.EXIT_CODE_REBOOT
def slotReboot(self):
print "Performing application reboot.."
qApp.exit( self.EXIT_CODE_REBOOT )
def main():
currentExitCode = 0
app = QApplication(sys.argv)
ex = MyAppMainWindow()
while currentExitCode == ex.EXIT_CODE_REBOOT :
currentExitCode = app.exec_()
return currentExitCode
if __name__ == '__main__':
main()
Obviously I didn't fully understand. Pleas help.
There's a nice way presented by Qt Wiki on how to make your application restartable.
The approach is based on recreation of QApplication instance and not killing current process.
It can be easily adopted to PySide like the next snippet shows:
EXIT_CODE_REBOOT = -15123123 # you can use any unique value here
exit_code = EXIT_CODE_REBOOT # Just for making cycle run for the first time
while exit_code == EXIT_CODE_REBOOT:
exit_code = 0 # error code - no errors happened
app = QApplication(sys.argv)
...
exit_code = app.exec()
You just need to setup proper exit code through the API presenteed by QApplication before finishing your app. The you can hook up new configuration or whatever you need when new application instance is created.
The topic is quite old, but i see there is no proper solution provided. So here it is (based on what Rostyslav Dzinko suggested):
EXIT_CODE_REBOOT = -11231351
from PySide import QtGui, QtCore
import sys
class MyApp(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyApp, self).__init__(parent)
def restart (self):
#DO stuff before restarting here
return QtCore.QCoreApplication.exit( EXIT_CODE_REBOOT )
def start_app():
exit_code = 0
while True:
try:
app = QtGui.QApplication(sys.argv)
except RuntimeError:
app = QtCore.QCoreApplication.instance()
myap = MyApp()
myap.show()
exit_code = app.exec_()
if exit_code != EXIT_CODE_REBOOT:
break
return exit_code
if __name__ == '__main__':
start_app()