When I run the basic script:
import sys
from PySide2.QtWidgets import QApplication, QLabel
app = QApplication(sys.argv)
label = QLabel("Hello World")
label.show()
app.exec_()
forthe first time it all works fine. However, if I run it a second time I get:
File "../script.py", line 17, in <module>
app = QApplication(sys.argv)
RuntimeError: Please destroy the QApplication singleton before creating a new QApplication instance.
I am running the scripts in an Ubuntu machine. I get the same error in python2 and python3.
Thanks !
Probably your IDE has already created a QApplication, so the solution is to create a QApplication if it does not exist:
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
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()
This question already has answers here:
PySide2 not closing correctly with basic example
(1 answer)
Problems with Multiple QApplications
(3 answers)
Closed 3 years ago.
After execution of the following code
# ===
from PyQt5.QtWidgets import QApplication, QLabel
app = QApplication([])
label = QLabel('Hello World!')
label.show()
app.exec_()
# ===
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPalette
from PyQt5.QtWidgets import QApplication, QPushButton
app = QApplication([])
app.setStyle('Fusion')
palette = QPalette()
palette.setColor(QPalette.ButtonText, Qt.red)
app.setPalette(palette)
button = QPushButton('Hello World')
button.show()
app.exec_()
I receive the error:
Process finished with exit code -1073741819 (0xC0000005)
I am using Python 3.7, PyQt5 in PyCharm version 2019.1.3 and on Windows 10. I have reinstalled packages, reinstalled Pycharm, but to no avail.
It might be a Windows-specific bug.. or I am violating a PyQt-way-of-working by redefining the app-variable.
I can run both 'programs' individually by executing them in separate python environments, but not one after the other.
If I execute the code NOT in Pycharm I see that I either get kicked out of Python after defining app.setPalette(palette), having ran the first part. Or after label = QLabel('Hello World!'), after having ran the second part.
Any extra information as to WHY this is happening would be nice :) Since I dont understand that part.
Solution as to "just dont do that", won't help me in understanding the problem. Thanks in advance
Try it:
from PyQt5.QtWidgets import QApplication, QLabel, QPushButton
from PyQt5.QtCore import Qt, QRect, QSize
from PyQt5.QtGui import QPalette
def closeEvent():
app = QApplication([])
app.setStyle('Fusion')
palette = QPalette()
palette.setColor(QPalette.ButtonText, Qt.red)
app.setPalette(palette)
button = QPushButton('QPushButton: Hello World')
button.show()
app.exec_()
app = QApplication([])
app.aboutToQuit.connect(closeEvent) # +++
label = QLabel('QLabel: Hello World!')
label.show()
app.exec_()
Currently I can open a folder by using
dirPath = os.path.dirname(os.path.abspath(self.oVidPath))
QDesktopServices.openUrl(QUrl.fromLocalFile(dirPath))
I want to know if there is anyway I can open folder with a file preselected?
I am okay if it only works on linux systems (nautilus is preferred)
edit : This application is only going be for linux systems
For windows
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.Qt import QProcess
if __name__ == '__main__':
app = QApplication(sys.argv)
command = "explorer /e, /select, c:\\windows\\regedit.exe"
process = QProcess()
process.start(command)
sys.exit(app.exec_())
For Linux
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.Qt import QProcess
if __name__ == '__main__':
app = QApplication(sys.argv)
command = "nautilus /var/log/dpkg.log"
process = QProcess()
process.start(command)
sys.exit(app.exec_())
FYI https://askubuntu.com/a/82717/249546
This is PyQT code that I have to be executed in Spyder. The first time, I executed it, it works well. On the second time, it says:
QWidget: Must construct a QApplication before a QPaintDevice "
I searched for solution but nothing worked for me.
from PyQt4 import QtGui, QtCore
import sys
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.button = QtGui.QPushButton('Test', self)
self.button.clicked.connect(self.handleButton)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
print ('Hello World')
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
app.exec_()
#sys.exit(app.exec_())
I even commented sys.exit() which some people proposed. Could someone help me to get rid of this error as I am restarting the kernel every other time to execute.
First, your example is not really minimal. You'll observe, that
from PyQt4 import QtGui
if __name__ == '__main__':
app = QtGui.QApplication([])
w = QtGui.QWidget()
w.show()
app.exec_()
already does the trick.
My guess is that the console in which you let this script run twice is not deleting the QApplication (type app in the console you see the variable is still there).
In the second run, the newly created QApplication interferes with the still existing from the old run. They all run within the same console and it depends a bit on what spyder does when running a file.
To circumvent this delete the app before another run:
del app
I'd like to write an app in Python that will display website with WebGL content. That's my script:
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtCore import QFileInfo
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
webView = QtWebKit.QWebView()
webView.setGeometry(QtCore.QRect(30, 20, 591, 511))
webView.settings().setAttribute(QtWebKit.QWebSettings.WebGLEnabled, True)
webView.settings().setAttribute(QtWebKit.QWebSettings.AcceleratedCompositingEnabled, True)
webView.load(QtCore.QUrl('http://webglsamples.org/aquarium/aquarium.html'))
webView.show()
sys.exit(app.exec_())
It's like the "WebGLEnabled" setting doesn't work.
Do you have any ideas? Maybe you have some example code working in PyQT or PySide?