I am developing an app that when started needs to check various things like if the modules are found, if a connection to a microcontroller is working, etc. So I decided to make a splash screen where each of these steps show a message. What I thought was to make several images that have the text saying ('Loading modules...'), ('Checking connection...') and change them everytime the step is being run. The following code is what I thought of:
images = ["path1", "path2", ...]
app = QApplication(sys.argv)
for i in images:
pixmap = QPixmap(i)
splash = QSplashScreen(pixmap)
splash.show()
QTimer.singleShot(3000, splash.close) #the timer is just to pretend some process is running
app.processEvents()
app.exec_()
This code only shows the last of the images, not each separated by 3 seconds. Any clue on how I can fix this?
You must create signals that indicate the states and change the images:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Manager(QtCore.QObject):
"""Class that simulates the loading steps"""
started = QtCore.pyqtSignal()
loaded_modules = QtCore.pyqtSignal()
connected = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
def start(self):
self.started.emit()
for i, step in enumerate((self.loaded_modules, self.connect, self.finish), 1):
QtCore.QTimer.singleShot(i * 2000, step)
def load_modules(self):
self.loaded_modules.emit()
def connect(self):
self.connected.emit()
def finish(self):
self.finished.emit()
class SplashScreen(QtWidgets.QSplashScreen):
#QtCore.pyqtSlot()
def handle_started(self):
self.show()
self.setPixmap(QtGui.QPixmap("started.png"))
#QtCore.pyqtSlot()
def handle_loaded_modules(self):
self.setPixmap(QtGui.QPixmap("loaded_modules.png"))
#QtCore.pyqtSlot()
def handle_connected(self):
self.setPixmap(QtGui.QPixmap("connected.png"))
#QtCore.pyqtSlot()
def handle_finished(self):
self.close()
def main():
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QMainWindow()
w.resize(640, 480)
manager = Manager()
splash_screen = SplashScreen()
manager.started.connect(splash_screen.handle_started)
manager.loaded_modules.connect(splash_screen.handle_loaded_modules)
manager.connected.connect(splash_screen.handle_connected)
manager.finished.connect(splash_screen.handle_finished)
manager.finished.connect(w.show)
manager.start()
app.exec_()
if __name__ == "__main__":
main()
Related
I am trying to display a loading gif after a button is pressed. This is the code I currently have
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MainWindow (QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow,self).__init__(parent)
self.setGeometry(50,50,240,320)
self.home()
def home(self):
but = QtGui.QPushButton("Example", self)#Creates the brew coffee button
but.clicked.connect(self.gif_display)
but.resize(200,80)
but.move(20,50)
self.show()
def gif_display(self):
l = QMovieLabel('loading.gif')
l.show()
class QMovieLabel(QLabel):
def __init__(self, fileName):
QLabel.__init__(self)
m = QMovie(fileName)
m.start()
self.setMovie(m)
def setMovie(self, movie):
QLabel.setMovie(self, movie)
s=movie.currentImage().size()
self._movieWidth = s.width()
self._movieHeight = s.height()
def run():
app = QtGui.QApplication(sys.argv)
GUI = MainWindow()
sys.exit(app.exec_())
run()
I would like to display the gif called "loading.gif" after the button is pressed. Nothing appears after pressing the button and I am unsure of what to do to get the gif to properly appear. The gif is the same size as the screen that I created (240x320).
The problem is that QMovieLabel is a local variable within gif_display so it will be deleted when the function finishes running, so the solution is to avoid deleting it. There are 2 options: make it an attribute of the class or make it a child of the window , I will show the second method since I think it is the one you want:
import sys
from PyQt4 import QtCore, QtGui
class MainWindow (QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow,self).__init__(parent)
self.setGeometry(50,50,240,320)
self.home()
def home(self):
but = QtGui.QPushButton("Example", self) # Creates the brew coffee button
but.clicked.connect(self.gif_display)
but.resize(200,80)
but.move(20,50)
self.show()
#QtCore.pyqtSlot()
def gif_display(self):
l = QMovieLabel('loading.gif', self)
l.adjustSize()
l.show()
class QMovieLabel(QtGui.QLabel):
def __init__(self, fileName, parent=None):
super(QMovieLabel, self).__init__(parent)
m = QtGui.QMovie(fileName)
self.setMovie(m)
m.start()
def setMovie(self, movie):
super(QMovieLabel, self).setMovie(movie)
s=movie.currentImage().size()
self._movieWidth = s.width()
self._movieHeight = s.height()
def run():
app = QtGui.QApplication(sys.argv)
GUI = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
run()
I have a widget that ends with pressing OK button. I want that the widget returns a value to the main program (which is in simple example below the obtained value increased by 1). How can I do this?
Also, is there any more elegant way to show the same QWidget with the different title?
MWE:
import sys
from PyQt5 import QtGui, QtCore, QtWidgets
class MainWindow(QtWidgets.QWidget):
def __init__(self,val):
self.val=val
super(MainWindow, self).__init__()
self.initUI()
def initUI(self):
self.End= QtWidgets.QPushButton('OK', self)
self.End.clicked.connect(self.end)
MainLayout = QtWidgets.QVBoxLayout()
MainLayout.addWidget(self.End)
self.setLayout(MainLayout)
self.setWindowTitle(str(self.val))
self.show()
def end(self):
# return self.val+1
self.close()
def main():
app = QtWidgets.QApplication(sys.argv)
for i in range(10):
ex = MainWindow(i)
ex.show()
res = app.exec_()
print(res)
sys.exit()
if __name__ == '__main__':
main()
If you want to use a widget to get some value after some processing, the correct thing is to use QDialog, that prevents any other window from opening if you use exec_():
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Dialog(QtWidgets.QDialog):
def __init__(self, val, parent=None):
super(Dialog, self).__init__(parent)
self.val = val
self.initUI()
def initUI(self):
endButton = QtWidgets.QPushButton('OK')
endButton.clicked.connect(self.on_clicked)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(endButton)
self.setWindowTitle(str(self.val))
#QtCore.pyqtSlot()
def on_clicked(self):
self.val += 1
self.accept()
def main():
app = QtWidgets.QApplication(sys.argv)
for i in range(10):
ex = Dialog(i)
ex.setAttribute(QtCore.Qt.WA_DeleteOnClose)
if ex.exec_() == QtWidgets.QDialog.Accepted:
print(ex.val)
if __name__ == '__main__':
main()
It is unclear what exactly your intention is with this code. If val is supposed to be a counter for the number of MainWindow instances, then you must declare it as a static:
class MainWindow(QtWidgets.QWidget):
val = 0
def __init__(self):
super(MainWindow, self).__init__()
MainWindow.val += 1
self.initUI()
def initUI(self):
# ...
self.setWindowTitle(str(MainWindow.val))
# ...
def main():
# ...
for i in range(10):
ex = MainWindow()
# ...
# ...
There is nothing wrong in calling QWidget::setWindowTitle() so I don't see a problem here.
If you really want to have it as a non-static class member and each MainWindow uses it as some sort of ID, you can easily call the value right after the ex.show():
def main():
app = QtWidgets.QApplication(sys.argv)
for i in range(10):
ex = MainWindow()
ex.show()
print(MainWindow.val)
res = app.exec_()
print(res)
sys.exit()
Just because you close a widget doesn't mean you have destroyed it so calling the values stored inside the object is not an issue.
However it appears you are not familiar what QApplication::exec() is intended for. It runs the main loop of the given application and returns the exit status (you can set it when calling exit() as the parameter passed to that function). If you simply want to get the value of val upon closing a widget you can simply override the closeEvent handler and emit a signal. Create a QObject instance and connect each MainWindow instance's custom close signal to a slot and do with the value whatever you want to.
I am trying to display an image from another thread in the main thread or possibly even a new thread. The problem is I cannot get the image to show anywhere else. I am using the thread _thread to download the image so it won't freeze the gui but I can't seem to get it out of there properly using QImage or QPixmap.
I know how to use strings and such in signals as shown in the example below but I am not sure how to send the image in a signal properly. I have tried using requests to grab an image url and send it to the main which works but I wasn't able to fetch the string in the main and then just download it there real fast either. That also is not a good option due to freezing the GUI momentarily.
This code is an example that shows that I can do all of the work and download the image in the main if I want to but would rather not.
import urllib2
from PyQt4 import QtGui
from PyQt4.QtCore import QThread, pyqtSignal
from PyQt4.QtGui import QApplication, QPixmap
import sys
class MainWindow(QtGui.QWidget):
def __init__(self):
super(self.__class__, self).__init__()
layout = QtGui.QVBoxLayout(self)
self._Button = QtGui.QPushButton()
self._Button.setObjectName("_Button")
self.line_edit = QtGui.QLineEdit()
self.line_edit.setObjectName("line_edit")
self.txt_box = QtGui.QTextEdit()
self.txt_box.setObjectName("txt_Box")
self.img_label = QtGui.QLabel()
self.img_label.setObjectName("img_label")
layout.addWidget(self._Button)
layout.addWidget(self.line_edit)
layout.addWidget(self.img_label)
layout.addWidget(self.txt_box)
self.pixmap = QPixmap()
self._thread = test_thread()
self._Button.clicked.connect(self.start_thread)
self.get_Text = self._thread.test_Signal.connect(self.line_edit.setText)
self.urls = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png'
data = urllib2.urlopen(self.urls).read()
self.pixmap.loadFromData(data)
self.img_label.setPixmap(self.pixmap)
def start_thread(self):
self._thread.start()
self._thread.test_Signal.connect(self.txt_box.setText)
class test_thread(QThread):
test_Signal = pyqtSignal(str)
def __init__(self):
super(test_thread, self).__init__()
def run(self):
string = 'This is a test string.'
self.test_Signal.emit(string)
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
And this code shows more of what I am looking to do.
import urllib2
from PyQt4 import QtGui
from PyQt4.QtCore import QThread, pyqtSignal
from PyQt4.QtGui import QApplication, QPixmap
import sys
class MainWindow(QtGui.QWidget):
def __init__(self):
super(self.__class__, self).__init__()
layout = QtGui.QVBoxLayout(self)
self._Button = QtGui.QPushButton()
self._Button.setObjectName("_Button")
self.line_edit = QtGui.QLineEdit()
self.line_edit.setObjectName("line_edit")
self.txt_box = QtGui.QTextEdit()
self.txt_box.setObjectName("txt_Box")
self.img_label = QtGui.QLabel()
self.img_label.setObjectName("img_label")
layout.addWidget(self._Button)
layout.addWidget(self.line_edit)
layout.addWidget(self.img_label)
layout.addWidget(self.txt_box)
self.pixmap = QPixmap()
self._thread = test_thread()
self._Button.clicked.connect(self.start_thread)
self.get_Text = self._thread.test_Signal.connect(self.line_edit.setText)
"...Send signal here and show image. EX:"
"...self._thread.image_Signal.connect(self.img_label.setPixmap)...."
"...or...."
def show_image_here(self):
"....Send image here after downloaded in thread...."
def start_thread(self):
self._thread.start()
self._thread.test_Signal.connect(self.txt_box.setText)
class test_thread(QThread):
test_Signal = pyqtSignal(str)
"....Example image signal...."
"....image_Signal = pyqtSignal()...."
def __init__(self):
super(test_thread, self).__init__()
self.pixmap = QPixmap()
def run(self):
string = 'This is a test string.'
self.test_Signal.emit(string)
self.urls = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png'
data = urllib2.urlopen(self.urls).read()
self.pixmap.loadFromData(data)
"....Not sure what to do here...."
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
Can somebody help me out? Thanks.
After looking around some more I came across this thread PySide threading and http downloading
I wish I had found it sooner because I did a lot of searching before my post. The person who answered that thread reclosedev also posted this in the comments:
https://pastebin.com/b4MD5jKh
Which helped me immensely to understand some things better. I used his code and slightly altered it to fit mine and it will work great for what I would like to do.
Here is my full working code using my original example for anyone out there that may find it useful.
import urllib2
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QThread, pyqtSignal
from PyQt4.QtGui import QApplication
import sys
class MainWindow(QtGui.QWidget):
def __init__(self):
super(self.__class__, self).__init__()
layout = QtGui.QVBoxLayout(self)
self._Button = QtGui.QPushButton()
self._Button.setObjectName("_Button")
self.line_edit = QtGui.QLineEdit()
self.line_edit.setObjectName("line_edit")
self.txt_box = QtGui.QTextEdit()
self.txt_box.setObjectName("txt_Box")
self.img_label = QtGui.QLabel()
self.img_label.setObjectName("img_label")
self.imagepreview = ImagePreview()
layout.addWidget(self._Button)
layout.addWidget(self.line_edit)
layout.addWidget(self.img_label)
layout.addWidget(self.txt_box)
layout.addWidget(self.imagepreview)
self._thread = test_thread()
self._Button.clicked.connect(self.start_thread)
self._Button.clicked.connect(self.imagepreview.startDownload)
self.get_Text = self._thread.test_Signal.connect(self.line_edit.setText)
def start_thread(self):
self._thread.start()
self._thread.test_Signal.connect(self.txt_box.setText)
class test_thread(QThread):
test_Signal = pyqtSignal(str)
def __init__(self):
super(test_thread, self).__init__()
def run(self):
string = 'This is a test string.'
self.test_Signal.emit(string)
class DownloadThread(QtCore.QThread):
data_downloaded = pyqtSignal()
def __init__(self, url):
QtCore.QThread.__init__(self)
self.url = url
self._data = None
def run(self):
self._data = urllib2.urlopen(self.url).read()
self.data_downloaded.emit()
def get_data(self):
return self._data
class ImagePreview(QtGui.QWidget):
def __init__(self, parent=None):
super(ImagePreview, self).__init__(parent)
self.setMinimumSize(50, 50)
self.pixmap = None
def paintEvent(self, paintEvent):
painter = QtGui.QPainter(self)
if (self.pixmap):
painter.drawPixmap(0, 0, self.pixmap)
def startDownload(self):
self.download_thread = DownloadThread('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png')
self.download_thread.start()
self.download_thread.data_downloaded.connect(self.ondownloadFinished)
def ondownloadFinished(self):
self.paintImage()
print("download finished")
def paintImage(self):
print("Painting")
pixmap = QtGui.QPixmap()
pixmap.loadFromData(self.download_thread.get_data())
self.setPixmap(pixmap)
def setPixmap(self, pixmap):
self.pixmap = pixmap
self.setMinimumSize(pixmap.width(), pixmap.height())
self.update()
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
I am totally stuck with this python qt threading tutorial. I have two issues.
1) The longRunning function never executes.
2) When exiting the app, I get QThread: Destroyed while thread is still running and a Python.exe has stopped working.
Any help? Thanks!!
#!/usr/bin/env python2
import sys, time
from PySide.QtGui import *
from PySide import QtCore
class SeperateThread(QtCore.QObject):
finished = QtCore.Signal()
def __init__(self, parent = None):
super(SeperateThread, self).__init__(parent)
self._isRunning = True
def longRunning(self):
end = time.time()+3
while self._isRunning == True:
sys.stdout.write('*')
sys.stdout.flush()
time.sleep(1)
now = time.time()
if now>=end:
self._isRunning=False
self.finished.emit()
def stop(self):
self._isRunning = False
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self,parent)
centralwidget = QWidget(self)
startButton = QPushButton('Start long (3 seconds) operation',self)
label2 = QLabel('Long batch')
vbox = QVBoxLayout()
vbox.addWidget(startButton)
vbox.addWidget(label2)
self.setCentralWidget(centralwidget)
centralwidget.setLayout(vbox)
obj = SeperateThread()
objThread = QtCore.QThread()
obj.moveToThread(objThread)
objThread.started.connect(obj.longRunning)
def LongOperationStart():
label2.setText('app is running')
startButton.setEnabled(False)
objThread.start()
def LongOperationFinish():
startButton.setEnabled(True)
#GUI start button
startButton.clicked.connect(LongOperationStart)
#Once thread is finished.
objThread.finished.connect(LongOperationFinish)
obj.finished.connect(objThread.quit)
if __name__=='__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Oh ok, I found out what I was doing wrong:
SeperateThread is being garbage collected after the MainWindow constructor returns. Make obj a member of MainWindow so that it persists beyond the constructor.
Thanks!!
I am new to gui designing with pyqt..
I want to use a gif in the splash screen.. using this code..
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.browser = QTextBrowser()
self.setWindowTitle('Just a dialog')
if __name__ == "__main__":
import sys, time
app = QApplication(sys.argv)
# Create and display the splash screen
splash_pix = QPixmap('a.gif')
splash = QSplashScreen(splash_pix, Qt.WindowStaysOnTopHint)
splash.setMask(splash_pix.mask())
#splash.raise_()
splash.show()
app.processEvents()
# Simulate something that takes time
time.sleep(2)
form = Form()
form.show()
splash.finish(form)
app.exec_()
But i am getting only the first frame of the gif...How do i overcome this...?
Here what I got:
#!/usr/bin/env python2
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from multiprocessing import Pool
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.browser = QTextBrowser()
self.setWindowTitle('Just a dialog')
class MySplashScreen(QSplashScreen):
def __init__(self, animation, flags):
# run event dispatching in another thread
QSplashScreen.__init__(self, QPixmap(), flags)
self.movie = QMovie(animation)
self.connect(self.movie, SIGNAL('frameChanged(int)'), SLOT('onNextFrame()'))
self.movie.start()
#pyqtSlot()
def onNextFrame(self):
pixmap = self.movie.currentPixmap()
self.setPixmap(pixmap)
self.setMask(pixmap.mask())
# Put your initialization code here
def longInitialization(arg):
time.sleep(arg)
return 0
if __name__ == "__main__":
import sys, time
app = QApplication(sys.argv)
# Create and display the splash screen
# splash_pix = QPixmap('a.gif')
splash = MySplashScreen('a.gif', Qt.WindowStaysOnTopHint)
# splash.setMask(splash_pix.mask())
#splash.raise_()
splash.show()
app.processEvents()
# this event loop is needed for dispatching of Qt events
initLoop = QEventLoop()
pool = Pool(processes=1)
pool.apply_async(longInitialization, [2], callback=lambda exitCode: initLoop.exit(exitCode))
initLoop.exec_()
form = Form()
form.show()
splash.finish(form)
app.exec_()
Note, that you need to run your initialization code in a separate thread, since the main thread should dispatch Qt events.
Translated into PySide:
from PySide import QtCore
from PySide import QtGui
from multiprocessing import Pool
class Form(QtGui.QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.browser = QtGui.QTextBrowser()
self.setWindowTitle('Just a dialog')
class MySplashScreen(QtGui.QSplashScreen):
def __init__(self, animation, flags):
# run event dispatching in another thread
QtGui.QSplashScreen.__init__(self, QtGui.QPixmap(), flags)
self.movie = QtGui.QMovie(animation)
self.movie.frameChanged.connect(self.onNextFrame)
#self.connect(self.movie, SIGNAL('frameChanged(int)'), SLOT('onNextFrame()'))
self.movie.start()
def onNextFrame(self):
pixmap = self.movie.currentPixmap()
self.setPixmap(pixmap)
self.setMask(pixmap.mask())
# Put your initialization code here
def longInitialization(arg):
time.sleep(arg)
return 0
if __name__ == "__main__":
import sys, time
app = QtGui.QApplication(sys.argv)
# Create and display the splash screen
# splash_pix = QPixmap('a.gif')
splash = MySplashScreen('a.gif', QtCore.Qt.WindowStaysOnTopHint)
# splash.setMask(splash_pix.mask())
#splash.raise_()
splash.show()
app.processEvents()
# this event loop is needed for dispatching of Qt events
initLoop = QtCore.QEventLoop()
pool = Pool(processes=1)
pool.apply_async(longInitialization, [2], callback=lambda exitCode: initLoop.exit(exitCode))
initLoop.exec_()
form = Form()
form.show()
splash.finish(form)
app.exec_()