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_())
Related
This question already has answers here:
PyQt run time issue
(3 answers)
Disabling QPushButton before a task
(1 answer)
PyQT: PushButton receives commands while disabled
(1 answer)
Closed 21 days ago.
During handling of a clicked signal I would like to show a wait cursor until this is done.
All mouse clicks on other widgets should be ignored during that time.
The cursor part is working fine using setCursor().
But all mouse clicks on other buttons (or the original one) are still detected and cached, so they are handled after the first button
operation is finished.
How can I make sure that no user interface events are generated until my operation is finished?
This code shows the things I have already tried:
pushButton_B.setEnabled()
pushButton_B.clicked.disconnect()
pushButton_B.blockSignals()
None of those have led to the desired behaviour.
I am hoping for some way to disable user interface events for the whole window, so I don't need to do this for every
widget I introduce, but I couldn't find the right methods for this.
I'd appreciate any hints on how to do this properly.
import sys
import time
from PySide6.QtCore import Qt, QRect, QCoreApplication, QMetaObject
from PySide6.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton
class Ui_MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.resize(200, 200)
self.centralwidget = QWidget(self)
self.pushButton_A = QPushButton(self.centralwidget)
self.pushButton_A.setText('A')
self.pushButton_A.setGeometry(QRect(20, 20, 100, 24))
self.pushButton_B = QPushButton(self.centralwidget)
self.pushButton_B.setText('B')
self.pushButton_B.setGeometry(QRect(20, 60, 100, 24))
self.setCentralWidget(self.centralwidget)
self.pushButton_A.clicked.connect(self.pushButton_A_clicked)
self.pushButton_B.clicked.connect(self.pushButton_B_clicked)
def pushButton_A_clicked(self):
print("pushButton_A clicked...")
self.setCursor(Qt.CursorShape.WaitCursor)
self.pushButton_B.setEnabled(False)
self.pushButton_B.clicked.disconnect(self.pushButton_B_clicked)
self.pushButton_B.blockSignals(True)
self.repaint()
time.sleep(3) # Some lengthy operation
self.setCursor(Qt.CursorShape.ArrowCursor)
self.pushButton_B.setEnabled(True)
self.pushButton_B.clicked.connect(self.pushButton_B_clicked)
self.pushButton_B.blockSignals(False)
print("pushButton_A done.")
def pushButton_B_clicked(self):
print("pushButton_B clicked.")
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Ui_MainWindow()
w.show()
app.exec()
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_()
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_() )
I have a GUI that was generated using Qt Designer, I used pyuic5 to generate a .py file. In a separate py (program.py) file I import my UI a do all my work there.
program.py
import sys, os, time
from subprocess import call
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyCred_GUI import Ui_Dialog
class MyGUI(Ui_Dialog):
def __init__(self, dialog):
Ui_Dialog.__init__(self)
self.setupUi(dialog)
self.pushButton_2.clicked.connect(self.cancelbutton)
def cancelbutton(self):
exit()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog = QtWidgets.QDialog()
dialog.setWindowFlags(QtCore.Qt.WindowSystemMenuHint)
prog = MyGUI(dialog)
dialog.show()
sys.exit(app.exec_())
I pulled a lot out just to focus on the issue here. When I click my Cancel button, I want the window to hide, set a timer, and then reappear after so many seconds. I have tried every combination of self.close() self.hide() self.destroy() and none of them hide my window. I get an error that says
"AttributeError: 'MyGUI' object has no attribute 'hide'"
Which makes sense because MyGUI doesn't have a hide() function. I am at a complete loss on how to hide this window.
EDIT (Solved)
For future people, as suggested by Hi Im Frogatto dialog.hide() worked.
In your code snippet, dialog is of type QDialog and thereby having hide method. However instances of MyGUI class seem to not have such a method. So, if you write dialog.hide() in that __init__() function, you can hide it.
When I am using other programs (e.g. opening a pdf or word), I will select some text contents (like a word or paragraph) by using the mouse. I want my python program to get this text content. How to do this using PyQt, or some other Python library?
This is an easy task, you haven't specified the pyqt version, so I'll post the solution for PyQt4, here you go:
from PyQt4.QtCore import QObject, pyqtSlot, SIGNAL, SLOT
from PyQt4.QtGui import QApplication, QMessageBox
import sys
class MyClipboard(QObject):
#pyqtSlot()
def changedSlot(self):
if(QApplication.clipboard().mimeData().hasText()):
QMessageBox.information(None, "Text has been copied somewhere!",
QApplication.clipboard().text())
def main():
app = QApplication(sys.argv)
listener = MyClipboard()
app.setQuitOnLastWindowClosed(False)
QObject.connect(QApplication.clipboard(), SIGNAL(
"dataChanged()"), listener, SLOT("changedSlot()"))
sys.exit(app.exec_())
if __name__ == '__main__':
main()