PyQt5 gives error code after execution of example code [duplicate] - python

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_()

Related

PyQt6 Accessing UI Object Attributes

I'm trying to create a simple GUI in Python using PyQt6 and Qt Designer.
I've already made the .ui file, and am able to load it properly and have it shown when running the code.
I can't, however, seem able to access any of the objects, i.e buttons and such.
When I print out the children of the AppWindow object I do see all of them listed, but I can't change their attributes via self.button (inside the class init function)- it says 'unresolved attribute reference'.
To my understanding it's supposed to be something trivial, so I'm not quite sure what I had managed to get wrong here.
Would appreciate any help since I'm stumped.
Thank you.
Here's my code-
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout
from PyQt6.QtCore import Qt
from PyQt6 import uic
class AppWindow(QWidget):
def __init__(self):
super().__init__()
uic.loadUi('gui.ui', self)
self.setWindowTitle('API-SF App')
if __name__ == '__main__':
app = QApplication(sys.argv)
app_window = AppWindow()
app_window.show()
sys.exit(app.exec())

PyQT5's input application dialogue box is not staying on windows 10 screen

I am developing a PyQt5 application which takes an input from the user in a string format and then utilise that variable further in the code.
Problem: The input box code when called from within a while loop (Ideally the box should stay and wait for the input from the user, thereby holding the while loop execution as well), instead it does not stay on the screen, it flashes and disappears in a fraction of seconds when executing the script on windows 10. But when I execute the code snippet mentioned below separately, then this type of problem does not appear.
Code Snippet
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QLabel
def call_qt():
app = QApplication(sys.argv)
gui = QWidget()
text, ok = QInputDialog.getText(gui, "User Input",
"""Do you wish to Continue [Y/N]?""")
#print(text, ok)
if ok:
app.exit()
else:
app.exit()
return text
print(call_qt())
I am not able to figure out, What could be the problem with this code snippet. Could you please help me with this? Also, I am new to PyQt5.
Confusion: The same problem does not exist on Ubuntu 18.
The problem with this part is the process handling in windows. Do threading of the QT application and call this thread inside your while loop. This should solve the problem.
from queue import Queue
que = Queue()
def call_qt(out_que):
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QLabel
app = QApplication(sys.argv)
gui = QWidget()
text, ok = QInputDialog.getText(gui, "User Input",
"""Do you wish to Continue [Y/N]?""")
#print(text, ok)
if ok:
app.exit()
else:
app.exit()
out_que.put()
while True:
t = Threading.thread(target=call_qt, args=(que,))
t.start()
t.join()
print("text: ",que.get())
same problem should happen on ubuntu. As you hit OK, application terminates itself and you wont be able to see output. Try this code, it prints the result on widget
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QInputDialog, QVBoxLayout, QLabel)
def call_qt(main_widow):
text, ok = QInputDialog.getText(main_widow, "User Input", "Do you wish to Continue [Y/N]?")
return text, ok
if __name__ == '__main__':
app = QApplication(sys.argv)
main_widow = QWidget()
layout = QVBoxLayout()
label = QLabel()
layout.addWidget(label)
main_widow.setLayout(layout)
main_widow.show()
text, ok = call_qt(main_widow)
# if ok:
# sys.exit()
label.setText(text)
sys.exit(app.exec_())

How to use PyQt5 to set system clipboard without event loop? [duplicate]

This question already has answers here:
How can I disable clear of clipboard on exit of PyQt application?
(2 answers)
Image copied to clipboard doesn't persist on Linux
(1 answer)
Closed 3 years ago.
How to use Pyqt5 to set system clipboard and quit as soon as possible? QCoreApplication doesn't come with clipboard support, so the choice should be QApplication.
The following code is not working without starting event loop app.exec_(). But all I need is to set the clipboard and quit the script as soon as possible.
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication([])
app.clipboard().setText('test')
# sys.exit(app.exec_())
I follow the instruction from How can I disable clear of clipboard on exit of PyQt application.
The following two methods don't work either.
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QEvent
app = QApplication([])
cb = QApplication.clipboard()
cb.setText('test01')
event = QEvent(QEvent.Clipboard)
app.sendEvent(cb, event)
app.processEvents()
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QEvent, QTimer
app = QApplication([])
cb = QApplication.clipboard()
cb.setText('test02')
QTimer.singleShot(0, app.quit)
sys.exit(app.exec_())

PySide2 not closing correctly with basic example

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)

How can I receive QLineEdit input when the login button is pressed? [duplicate]

This question already has answers here:
Name 'xxx' is not defined
(1 answer)
python NameError: name 'xxx' is not defined
(1 answer)
Closed 4 years ago.
I've watched every tutorial but found no luck, even tho I've tried everything. I've actually made a successful program with a login and all validations using Tkinter but as soon as i switched to PyQt5 for better design and more widgets, it's all of a sudden so confusing. I am a beginner programmer but I know more than the basics. It's just PyQt5 is somehow confusing to me and my Ui files can't convert to py for some reason so i can't use Qt designer.
That leads to my question, how can i receive username and password after the user clicks login.I also want to clear user input but that isn't important to me right now.
Please help I've been stuck on this for three days. The window and line edit is shown but nothing is working.
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget, QLineEdit, QLabel
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QIcon, QPixmap
class MainWindow(QMainWindow):
def __init__(login):
QMainWindow.__init__(login)
login.setMinimumSize(QSize(300, 195 ))
login.setWindowTitle("Login Window")
login.setStyleSheet("QLabel {font: 12pt Calibri}")
loginbtn = QtWidgets.QPushButton('Login', login)
loginbtn.clicked.connect(login.loginMethod)
loginbtn.resize(80,30)
loginbtn.move(60, 150)
clearbtn = QtWidgets.QPushButton('Clear', login)
clearbtn.clicked.connect(login.loginMethod)
clearbtn.resize(70,30)
clearbtn.move(170, 150)
###############USERNAME##########
userLabel=QLabel(login)
userLabel.setText("Username: ")
userLabel.move(10,20)
userinput=QtWidgets.QLineEdit(login)
userinput.move(90,20)
userinput.resize(150,30)
userinput.setPlaceholderText(" username")
#########PASSWORD##############
passLabel=QLabel(login)
passLabel.setText("Password: ")
passLabel.move(10,60)
passinput=QtWidgets.QLineEdit(login)
passinput.move(90,60)
passinput.resize(150,30)
def loginMethod(login):
sender=login.sender()
if sender.text()=="Login":
print(userinput.text())
else:
sender.text.clear()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )

Categories

Resources