Window spawned from MainWindow doesn't fully close when closing - python

I have a main window which creates a second window when a button is pressed. I have two problems that occur but for now I will focus on the first one: I cannot get it to completely close the old window when I close it. It closes it visually but the window is still running in the background. To illustrate this I put in a counter. Below is generic code which illustrates this issue.
Here is my dialog window:
from PyQt5 import QtCore, QtGui, QtWidgets
from form_test import Ui_Form
class formTest(QtWidgets.QWidget, Ui_Form):
def __init__(self):
QtWidgets.QWidget.__init__(self)
flags = QtCore.Qt.Drawer | QtCore.Qt.WindowStaysOnTopHint
self.setWindowFlags(flags)
self.setupUi(self)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(507, 305)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.btnOpenForm = QtWidgets.QPushButton(self.centralwidget)
self.btnOpenForm.setGeometry(QtCore.QRect(170, 30, 93, 28))
self.btnCloseForm = QtWidgets.QPushButton(self.centralwidget)
self.btnCloseForm.setGeometry(QtCore.QRect(170, 160, 93, 28))
MainWindow.setCentralWidget(self.centralwidget)
self.btnOpenForm.clicked.connect(self.openClicked)
self.btnCloseForm.clicked.connect(self.closeClicked)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.btnOpenForm.setText(_translate("MainWindow", "Open Form"))
self.btnCloseForm.setText(_translate("MainWindow", "Close Form"))
def openClicked(self):
print('open')
self.popForm = formTest()
self.popForm.show()
def closeClicked(self):
print('close')
self.popForm.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Here is my spawning windows code (form_test.py):
from PyQt5 import QtCore, QtGui, QtWidgets
import time, threading
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(190, 170, 93, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.pushButtonClicked)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
self.loopThread = threading.Thread(target = self.looping)
self.loopThread.daemon = True
self.loopThread.start()
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "PushButton"))
def pushButtonClicked(self):
print("clicked")
def looping(self):
count = 0
while(True):
print('looping... ' + str(count))
count = count + 1
time.sleep(1)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
When I close the second window it is still running in memory. When I try to open it again another one begins running and then I have two running. Is there a different window I should use which will destroy all memory when it is closed? The second issue I am having is which my actual code (not this generic code) it causes a crash when I try to launch a second time. That issue may go away with the fixing of this issue and since I am not able to share my code online I have to stay as generic as possible for now so I am hoping to solve this issue first and maybe get resolution too.
PLEASE help ... I've worked on this FAR too long!!! Thanks, in advance, for any assistance on this.

Related

pyqt5 send/show messages from different file/class

I have two files, "main.py", "debugger.py".
On "debugger.py" I have a "textEdit" where I want to get/show messages.
On "main.py" I import and instantiate the class from "debugger.py" but instead of just sending/showing the messages, it opens the "debugger.py" window UI and shows the messages, i just need to send the message without opening the window, how can I do that?
"debugger.py".
import os, time
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QTextCursor
class Debugger_MainWindow(object):
def __init__(self):
super().__init__()
#def print_text(self):
#self.txtEdit_debugger.insertPlainText('hi')
def Debugger_setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lbl_info = QtWidgets.QLabel(self.centralwidget)
self.lbl_info.setGeometry(QtCore.QRect(90, 450, 451, 61))
self.lbl_info.setObjectName("lbl_info")
self.lbl_tittle = QtWidgets.QLabel(self.centralwidget)
self.lbl_tittle.setGeometry(QtCore.QRect(280, 40, 56, 15))
self.lbl_tittle.setObjectName("lbl_tittle")
self.txtEdit_debugger = QtWidgets.QTextEdit(self.centralwidget)
#self.txtEdit_debugger.setEnabled(False)
self.txtEdit_debugger.setGeometry(QtCore.QRect(70, 80, 481, 361))
self.txtEdit_debugger.setObjectName("txtEdit_debugger")
self.txtEdit_debugger.setReadOnly(True)
##Check box on/off
self.chkb_on_off_debugger = QtWidgets.QCheckBox(self.centralwidget)
self.chkb_on_off_debugger.setGeometry(QtCore.QRect(90, 500, 30, 20))
self.chkb_on_off_debugger.setObjectName("chkb_on_off_debugger")
#self.chkb_on_off_debugger.stateChanged.connect(self.print_text)#Show logger messages
self.chkb_on_off_debugger.setCheckable(True)
self.lbl_on_off_debugger = QtWidgets.QLabel(self.centralwidget)
self.lbl_on_off_debugger.setGeometry(QtCore.QRect(110, 500, 60, 20))
self.lbl_on_off_debugger.setObjectName("lbl_on_off_debugger")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.lbl_info.setText(_translate("MainWindow", "This is read only mode. "))
self.lbl_tittle.setText(_translate("MainWindow", "Debugger"))
self.lbl_on_off_debugger.setText(_translate("MainWindow", "Show messages"))
self.lbl_on_off_debugger.adjustSize()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Debugger_MainWindow()
ui.Debugger_setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
"main.py"
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSlot, QThreadPool, QTimer
from PyQt5.QtGui import QColor, QKeySequence, QIcon, QTextCursor
from Debugger import Debugger_MainWindow
class Ui_MainWindow(object):
def __init__(self):
super().__init__()
###### Methods
#this method opens a new window, i just want to send the message
def print_text(self):
self.ui_debugger = Debugger_MainWindow()
self.ui_debugger.Debugger_setupUi(MainWindow)
self.ui_debugger.txtEdit_debugger.insertPlainText('hi')
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 500)
MainWindow.setStyleSheet(open("styles.qss", "r").read())
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
##Group box
self.gpb_spell_main = QtWidgets.QGroupBox(self.centralwidget)
self.gpb_spell_main.setGeometry(QtCore.QRect(10, 5, 290, 230))
self.gpb_spell_main.setObjectName("gpb_spell_main")
self.gpb_spell_main.setCheckable(True)
self.gpb_spell_main.setChecked(False)
##Button
spell_main_color = QColor(255, 0, 255)
self.btn_spell_main = QtWidgets.QPushButton(self.gpb_spell_main)
self.btn_spell_main.setGeometry(QtCore.QRect(10, 35, 85, 27))
self.btn_spell_main.setObjectName("btn_spell_main")
self.btn_spell_main.clicked.connect(self.print_text)
#self.btn_spell_main.setEnabled(False)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.gpb_spell_main.setTitle(_translate("HeallingWindow", "Spell Main"))
self.btn_spell_main.setText(_translate("MainWindow", "Click Here"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Note: I couldn't fix my issue reading similar questions. PyQt5 - Show QDialog from a different class
I don't know if I understand problem but you may have to create new QtWidgets.QMainWindow() to create separated Debug Window. If you use existing MainWindow then it replaces widgets in existing window.
You may also set None to Debug Window in __init__ and later check if Debug Window is not None - to create only one window.
class Ui_MainWindow(object):
def __init__(self):
super().__init__()
self.DebugWindow = None
def print_text(self):
if self.DebugWindow is None:
self.DebugWindow = QtWidgets.QMainWindow()
self.ui_debugger = Debugger_MainWindow()
self.ui_debugger.Debugger_setupUi(self.DebugWindow)
self.DebugWindow.show()
self.ui_debugger.txtEdit_debugger.insertPlainText('hi\n')
Other problem cam be when you close Debug Window because it will not opne it again. It may need to change code in Debug Window and assign function which will hide it instead of destroing it.

How to open MainWindow from a SplashScreen without button in Pyqt5?

I am trying to create a loading screen because my MainWindow takes over 10 seconds to open, which is ok as long as I have a loading screen.
Here I have some code and when i run it, it will open a loading screen.
After the loading screen pops up i want it to immediately try to open the MainWindow. (On my actual app this would take 10 seconds to open)
I have used time.sleep to simulate the time my actual programme would take to open as this is just smaller code for you to read. However my real code will not include any sleep it will just naturally take 10 seconds to load.
How can I make it so once my loading screen is visible it will immediately open MainWindow? Currently I have not asked it to do anything, the loading screen just opens and that's it.
What extra code do I need to make it open MainWindow?
Ideally I would like to update the progress bar as this happens but that is not nessecary for now I just want to know how to open MainWindow immediately from the loading screen, thanks.
from PyQt5 import QtCore, QtGui, QtWidgets
import time
class Ui_SplashScreen(object):
def setupUi(self, SplashScreen):
SplashScreen.setObjectName("SplashScreen")
SplashScreen.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(SplashScreen)
self.centralwidget.setObjectName("centralwidget")
self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar.setGeometry(QtCore.QRect(190, 220, 461, 91))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
SplashScreen.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(SplashScreen)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
SplashScreen.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(SplashScreen)
self.statusbar.setObjectName("statusbar")
SplashScreen.setStatusBar(self.statusbar)
self.retranslateUi(SplashScreen)
QtCore.QMetaObject.connectSlotsByName(SplashScreen)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
time.sleep(10)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(310, 180, 341, 161))
font = QtGui.QFont()
font.setPointSize(40)
self.label.setFont(font)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "MY APP"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
SplashScreen = QtWidgets.QMainWindow()
ui = Ui_SplashScreen()
ui.setupUi(SplashScreen)
SplashScreen.show()
sys.exit(app.exec_())
You should not modify the code generated by pyuic so you must recreate those files that for my solution will be splash_ui.py and main_ui.py.
The idea is that the time consuming task should not run in the main thread but in a secondary thread and emit signals when it starts and ends that show and hide the splashscreen.
import threading
import sys
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtWidgets import QApplication, QMainWindow
from main_ui import Ui_MainWindow
from splash_ui import Ui_SplashScreen
def long_running_function():
import time
time.sleep(10)
class Worker(QObject):
started = pyqtSignal()
finished = pyqtSignal()
def start(self):
threading.Thread(target=self._execute, daemon=True).start()
def _execute(self):
self.started.emit()
# FIXME
long_running_function()
self.finished.emit()
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class SplashScreen(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_SplashScreen()
self.ui.setupUi(self)
def main():
app = QApplication(sys.argv)
splash_screen = SplashScreen()
main_window = MainWindow()
worker = Worker()
worker.started.connect(splash_screen.show)
worker.finished.connect(splash_screen.close)
worker.finished.connect(main_window.show)
worker.start()
sys.exit(app.exec_())

Change ClearButton Icon of QLineEdit

I want to change the ClearButton icon of my QLineEdit at Python 3.8 and PyQt5 (5.15.0) on Windows 10 (1909, 64-bit), later on I want to run the code on Linux.
I've tried to apply the code found here: How to make an extra icon in QLineEdit like this?
However, I'm not able to produce some running Python 3 code out of it and hope somebody can help me with my issue.
Below there is a preview of my minimal code example. Beside the QLineEdit with the standard ClearButton, the icon I actually want is visible - also to assure the resources_rc.py is loaded correctly.
My minimal code example consists of two .py files (one for the image and one for the code itself):
ClearButtonTest.py:
from PyQt5 import QtCore, QtGui, QtWidgets
import resources_rc
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(423, 136)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(32, 28, 257, 57))
font = QtGui.QFont()
font.setPointSize(14)
self.lineEdit.setFont(font)
self.lineEdit.setClearButtonEnabled(True)
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(324, 33, 33, 37))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/icon/ClearButton.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton.setIcon(icon)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
resources_rc.py:
from PyQt5 import QtCore
qt_resource_data = b"\
\x00\x00\x03\xfb\
\x89\
\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\
\x00\x00\x14\x00\x00\x00\x14\x08\x02\x00\x00\x00\x02\xeb\x8a\x5a\
\x00\x00\x00\x01\x73\x52\x47\x42\x00\xae\xce\x1c\xe9\x00\x00\x00\
\x04\x67\x41\x4d\x41\x00\x00\xb1\x8f\x0b\xfc\x61\x05\x00\x00\x00\
\x09\x70\x48\x59\x73\x00\x00\x0e\xc2\x00\x00\x0e\xc2\x01\x15\x28\
\x4a\x80\x00\x00\x03\x90\x49\x44\x41\x54\x38\x4f\x4d\x54\x4b\x4f\
\x1b\x57\x14\xc6\x8f\x99\xb1\xc0\x15\x89\x44\x23\xbb\xae\xc1\x06\
\xcc\x58\xbc\xec\xb1\x09\x41\xd4\x22\x62\xd1\x90\x45\xb3\x29\x31\
\x31\x4d\x52\x2a\x56\x89\x50\xd4\x15\xff\x22\xc1\x2c\x78\xa5\xab\
\x66\x41\xb3\x48\x37\xcd\x06\x55\x79\xa0\x1a\x92\x80\x94\xaa\x94\
\x0d\x34\x18\x08\xa9\xc2\xcc\x90\x48\x48\x78\x4c\x31\x73\xc7\xee\
\x77\xae\x41\xed\x99\xb9\x33\xf7\xea\x9c\xef\x9c\xef\x3c\x66\x6c\
\xa3\xa3\xa3\x1e\x8f\x67\xf7\xfd\xfb\xfc\xe1\xa1\x69\x32\x93\x1d\
\x5b\x58\xcc\x34\x4d\xcb\x62\xc7\x26\x76\x38\xe3\xc4\x18\x9e\xa4\
\x60\xcc\x29\x0a\xb5\x7e\xff\x9f\x7f\xac\xd8\x7d\x9f\xfb\x46\x46\
\x46\x6e\xdd\xbe\xed\x74\x38\x61\x6e\x59\x45\x86\x47\xf9\xc5\x8a\
\x16\x2e\xd3\xa2\x2d\x6e\xae\x16\x5d\xe2\x0f\xf7\xef\xcf\xce\xce\
\xb6\x45\xda\xed\x1f\x3e\x7e\xdc\xdf\xdf\x6f\x6c\x6c\xbc\xf3\xfd\
\x1d\xa7\xd3\x49\x56\x10\x56\x04\x90\xa3\xc8\x03\xb9\x00\x90\x59\
\xa2\x24\xce\x4c\xcf\x74\x74\x74\xa8\xaa\xba\xb3\xf3\xd6\x11\x69\
\x6f\xfb\xfd\xf5\xeb\xd6\xd6\xd6\x40\x5d\x40\x96\x9b\x5e\xbc\x78\
\x59\x38\x2a\x00\x56\xa4\x68\x04\x60\xa0\x0d\x17\x26\x13\x45\x71\
\x66\x66\x1a\xc8\x6c\x36\x9b\xec\xbf\xba\xab\xa9\x8e\x78\x3c\x9e\
\xcf\x1f\x2e\x2f\x2f\x47\x22\x91\x60\x30\x18\x0e\x87\x33\x99\x4c\
\xe1\xe8\x88\x63\x4f\x1d\x98\x96\x28\x0a\x53\xd3\xd3\xf1\x78\x47\
\x76\x33\x9b\x4c\x0e\xa8\xea\x2e\x54\x0e\x25\xa6\x40\x97\xcf\xe7\
\x5f\xbd\x7a\xa9\x28\xb1\xfa\xfa\xfa\xe6\xe6\xe6\xf9\xf9\xf9\x42\
\xa1\x00\x35\x0f\xca\x04\x41\x98\x9c\x9a\x42\x98\xcd\xcd\xcd\x6b\
\x03\x03\xea\xae\x5a\xf6\xea\x50\x22\x51\xd4\x15\x7b\xc3\xc8\x2f\
\x2e\x2e\xc2\x02\xf9\x23\x8b\xe7\xcf\x9e\xff\x53\x38\x82\x05\x62\
\x4e\x4e\x4e\x9e\x22\xaf\x69\xaa\x8a\x82\x13\xc2\x62\x94\x33\xe7\
\x47\xe5\xcd\xe5\x8d\xcc\x6f\x99\xf3\x9d\xe7\x9b\x9a\x9a\xda\xda\
\xdb\x9e\xfc\xfa\xc4\x66\xab\x98\x98\x98\x88\xc5\x62\x84\x4c\xa5\
\x54\x55\xa3\x1c\xca\x8b\x59\xb6\xeb\x37\xae\x53\x13\x79\x2f\x51\
\x16\xbc\xdd\xee\x4f\xd2\xe9\x74\x5d\x5d\xdd\xea\xea\x6a\xa9\x54\
\x42\x2d\x80\x4c\xa5\x52\x9a\xaa\xd1\x1c\xf0\x9e\x97\x37\x8e\x96\
\x96\x16\xcc\x01\x67\x41\x17\xc4\x30\x8c\x67\x4f\x9f\x7e\x91\x48\
\xc8\xb2\x8c\xf9\xd9\xde\xde\x4e\xa5\x06\x75\x5d\xe3\x69\xe2\x42\
\x1f\x88\x38\xc8\xda\xe9\x89\x23\xa1\xf9\xe2\x0d\xcd\x19\x46\x2e\
\x97\xab\xe0\x72\x70\x70\x60\xe4\x0e\xb8\x5b\x74\x1c\x5a\xa2\x8c\
\xbb\x54\x2c\xda\xcb\xd3\x40\x67\xe8\x79\x73\x45\x41\x1a\x1f\x1f\
\x47\xcd\x76\xb8\x80\xf6\x8f\x0f\x1e\x48\x92\x44\x55\x21\x4b\x1e\
\xaa\x68\xc1\xaf\x9d\x02\x51\xb4\x32\xdc\x12\x44\x29\x3d\x9e\x56\
\x14\x65\x6b\x6b\x6b\x70\xf0\x9b\x64\xf2\x2a\x46\xa2\xb3\xb3\xf3\
\xa7\x87\x0f\x5d\x92\x8b\x2c\xb8\x69\x99\x94\x9d\x93\x80\x50\xde\
\xa2\x20\xa6\xc7\xc6\x94\x68\x14\xc8\x9b\x37\xbf\xd5\x75\x55\xd3\
\xf5\xfe\xfe\xaf\x37\x36\x36\xba\xba\xba\x1e\xfd\xfc\xc8\xe5\x72\
\x21\x70\xa9\xa2\x74\x02\x26\x20\x77\x27\x0a\xc2\xbd\xb1\x7b\xd1\
\x68\x14\x15\x1a\x1a\x1a\xa2\x0a\xf1\x02\x68\xaa\x7e\xe5\xca\x57\
\x7f\xbd\x79\xd3\xdd\xdd\xfd\xcb\xe3\xc7\x95\x95\x95\x65\x24\x04\
\x39\x53\x64\x49\x10\xef\xde\x3d\x45\x7e\x57\x46\x52\xfe\xe4\xb9\
\xc8\xd0\xde\xcb\x7d\x97\xd7\xd7\xd7\x13\x89\xc4\xdc\xdc\x9c\xdb\
\xed\x3e\x41\xf7\xf6\xf6\x7e\x79\xe9\xd2\xca\xca\x0a\x5a\x0a\xb6\
\x3d\x3d\x3d\x21\x39\x14\xac\x0f\xfa\xfd\x7e\x8f\xc7\x5b\x53\x53\
\x53\x5d\x5d\x5d\x55\x55\x85\xaf\xc2\xeb\xf1\xae\xad\xad\xc1\x6c\
\x61\x61\xe1\x04\xdf\xd7\xd7\xf7\x1f\xf2\xe2\x45\x39\x2c\x37\x34\
\x34\xd4\xd6\xfa\xbd\x9f\x79\x3f\x3d\x57\x73\xe6\xec\x19\xd8\x49\
\x2e\xc9\x61\x77\xc0\x18\x6d\xff\x3f\xde\xee\xf3\xf9\x42\xa1\x10\
\xd8\x0e\x0f\x0f\xef\xed\xed\x51\xf6\x44\x16\xd9\x22\x21\x74\x85\
\xd2\xa6\xe6\xf0\xde\x68\x9a\x06\xa6\xe0\x8f\x04\xf1\xfd\xd9\x2e\
\x5c\xe8\x0a\x87\xe5\x77\x7f\xbf\xd3\x35\x1d\x83\xc3\xc7\xee\x18\
\x2f\x0c\x2b\xfd\x75\x4e\x7e\x43\x0c\xd1\x88\x27\x17\xc4\x0f\x04\
\x02\x4b\x4b\x4b\xff\x02\x87\x5b\x08\x31\xd5\xe7\xaf\xcd\x00\x00\
\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82\
"
qt_resource_name = b"\
\x00\x04\
\x00\x06\xfa\x5e\
\x00\x69\
\x00\x63\x00\x6f\x00\x6e\
\x00\x0f\
\x0c\x6d\x46\x87\
\x00\x43\
\x00\x6c\x00\x65\x00\x61\x00\x72\x00\x42\x00\x75\x00\x74\x00\x74\x00\x6f\x00\x6e\x00\x2e\x00\x70\x00\x6e\x00\x67\
"
qt_resource_struct_v1 = b"\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x02\
\x00\x00\x00\x0e\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
"
qt_resource_struct_v2 = b"\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\
\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x02\
\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x0e\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
\x00\x00\x01\x73\x2e\xd9\x74\x23\
"
qt_version = [int(v) for v in QtCore.qVersion().split('.')]
if qt_version < [5, 8, 0]:
rcc_version = 1
qt_resource_struct = qt_resource_struct_v1
else:
rcc_version = 2
qt_resource_struct = qt_resource_struct_v2
def qInitResources():
QtCore.qRegisterResourceData(rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data)
def qCleanupResources():
QtCore.qUnregisterResourceData(rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data)
qInitResources()
According to your code you have created a QPushButton with a custom icon instead of modifying the clear icon of the QLineEdit.
The solution is to access the QToolButton and set the icon:
self.lineEdit.findChild(QtWidgets.QToolButton).setIcon(
QtGui.QIcon(":/icon/ClearButton.png")
)
Or to QAction:
self.lineEdit.findChild(QtWidgets.QAction, "_q_qlineeditclearaction").setIcon(
QtGui.QIcon(":/icon/ClearButton.png")
)
Another solution is to use a QProxyStyle to change the default icon of the clear button:
class ProxyStyle(QtWidgets.QProxyStyle):
def standardIcon(self, standardIcon, option=None, widget=None):
if standardIcon == QtWidgets.QStyle.SP_LineEditClearButton:
return QtGui.QIcon(":/icon/ClearButton.png")
return super().standardIcon(standardIcon, option, widget)
app = QtWidgets.QApplication(sys.argv)
proxy_style = ProxyStyle(app.style())
app.setStyle(proxy_style)

Qtimer and closing out an applicaiton after 15 seconds have passed

Evening. I am having a bit of a hard time both understanding QTimer and how it works as well as getting the below code to work. As much as I hate copy/pasting, I just cannot figure it out. The goal of this is to run the program for 15 seconds and then exit out completely. Everything else works, but when I try to integrate QTimer to count how many seconds have passed, it does nothing or just simply does not work regardless of the variations I try. Below is the latest code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import QTimer
import winsound
import time
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(321, 249)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 320, 248))
self.label.setFrameShape(QtWidgets.QFrame.Box)
self.label.setOpenExternalLinks(False)
self.label.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#Plays the movie in the Label
movie = QMovie("giphy.gif")
self.label.setMovie(movie)
movie.start()
#Plays the sound with the movie
winsound.PlaySound("magicwrd.wav", winsound.SND_ASYNC|winsound.SND_LOOP)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
#Borrowed and modified from here: https://stackoverflow.com/questions/46656634/pyqt5-qtimer-count-until-specific-seconds
#Goal is to run the application for 15 seconds and then exit out completely.
def start_timer(self, slot, count=1, interval=1000):
counter = 0
def handler():
nonlocal counter
counter += 1
slot(counter)
if counter >= count:
timer.stop()
timer.deleteLater()
timer = QtCore.QTimer()
timer.timeout.connect(handler)
timer.start(interval)
def timer_func(self, count):
if count >= 5:
sys.exit(app.exec_())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
Ui_MainWindow.start_timer(Ui_MainWindow.timer_func, 5)
app.exec_()
If you want to finish the application in T seconds then you must use a QTimer::singleShot() so that when it is triggered then QCoreApplication::quit() is invoked:
QtCore.QTimer.singleShot(T * 1000, QtCore.QCoreApplication.quit)
In the example that you link, it has another objective: to print some information every T seconds and after repeating K times the application is closed, and therefore uses a complicated logic that you do not need.
Considering the above and the PyQt5 recommendation, the solution is:
import sys
import winsound
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(321, 249)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 320, 248))
self.label.setFrameShape(QtWidgets.QFrame.Box)
self.label.setOpenExternalLinks(False)
self.label.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
# Plays the movie in the Label
movie = QtGui.QMovie("giphy.gif")
self.label.setMovie(movie)
movie.start()
# Plays the sound with the movie
winsound.PlaySound("magicwrd.wav", winsound.SND_ASYNC | winsound.SND_LOOP)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
QtCore.QTimer.singleShot(15 * 1000, QtCore.QCoreApplication.quit)
sys.exit(app.exec_())

Change QLabel text dynamically not working

I am trying to change the QLabel text dymanically using QtDesigner, pyqt5.
Below is the code i am trying to use for changing the QLabel text dymanically.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label, 0, QtCore.Qt.AlignHCenter)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:28pt;\">" + self.getTime() + "</span></p></body></html>"))
def getTime(self):
time = QTime.currentTime().toString()
return time
def data(self):
time = QTime.currentTime().toString()
print("Time: " + time)
self.label.setText(time)
return time
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ex = Ui_MainWindow()
timer = QtCore.QTimer()
timer.timeout.connect(ex.data)
timer.start(1000) # 1 Second Refesh Rate
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
When i tried to run the code in QtDesigner the output window open's for a second and closed automatically. Not sure what is causing the output window to close. Please advise me to resolve the issue.
I recommend you execute your code in the terminal/CMD since many IDEs do not handle the Qt errors, if you do it you would get the following error:
Traceback (most recent call last):
File "main.py", line 33, in data
self.label.setText(time)
AttributeError: 'Ui_MainWindow' object has no attribute 'label
The error indicates that label does not exist, and that is correct because label is created after calling setupUi() but in your case "ex" does not call it. A possible solution is to first create the window and then start the timer:
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
timer = QtCore.QTimer()
timer.timeout.connect(ui.data)
timer.start(1000) # 1 Second Refesh Rate
sys.exit(app.exec_())
But a better solution is to follow the recommendations of PyQt(1) that states that you should not modify the class provided by Qt but use it as the widget interface:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label, 0, QtCore.Qt.AlignHCenter)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(
_translate(
"MainWindow",
'<html><head/><body><p><span style=" font-size:28pt;"></span></p></body></html>',
)
)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.data)
timer.start(1000)
self.data()
def data(self):
time_str = QTime.currentTime().toString()
self.label.setText(
'<html><head/><body><p><span style=" font-size:28pt;">{}</span></p></body></html>'.format(
time_str
)
)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
(1) Using the Generated Code
The problem is that you instatiate Ui_MainWindow twice: first as ex and later as ui. You connect the timer.timeout to ex, but call setupUi and show only for ui.
Try this:
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
timer = QtCore.QTimer()
timer.timeout.connect(ui.data)
timer.start(1000) # 1 Second Refesh Rate
sys.exit(app.exec_())
Qt Designer is only used for designing the UI, it does not allow running the program. Qt Creator can run programs, but only "real" Qt C++ programs.
You need to launch your .py scripts outside of Qt Designer, as if it were a normal Python script.

Categories

Resources