Pyqt5 open child windows of class of another file *.py Pb [duplicate] - python

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

Related

splash screen is blocked from ui dialog imports

I have made a big app importing a big number of dialogs all in main app(main loop).These dialogs import time is pretty long so i made a splash screen but ofcourse splash screen in main loop is blocked from the long time imports.The think i'm not getting is that i cant move the imports in main loop because i get an error from classes creating the ui , witch running as the code is checked from interpreter.
Here the sample code:
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
from PyQt5.QtWidgets import QDialog,QWidget,QApplication, QInputDialog, QLineEdit, QFileDialog,QProgressDialog, QMainWindow, QFrame,QSplashScreen
from PyQt5.QtCore import QThread , pyqtSignal,Qt
from PyQt5.QtGui import QIcon,QPainter,QPixmap
#here the slow import dialogs
from ui import Ui_MainWindow,HoverButton
from dialog1 import Ui_Dialog
from dialog2 import Ui_Dialog2
from dialog3 import Ui_dialog3
from dialog4 import Ui_Dialog4
from dialog5 import Ui_dialog5
from dialog6 import Ui_dialog6
#....... and so on
###after class methods###
class Dialog1(QtWidgets.QDialog,Ui_Dialog): #fuel button prompt dialog for inputs
def __init__(self,parent=None):
super(Dialog1, self).__init__(parent)
self.setupUi(self)
class Dialog2(QtWidgets.QDialog,Ui_Dialog2): #all errors dialog
def __init__(self,parent=None):
super(Dialog2, self).__init__(parent)
self.setupUi(self)
class Dialog3(QtWidgets.QDialog,Ui_dialog3): #that might take a while dialog
def __init__(self,parent=None):
super(Dialog3, self).__init__(parent)
self.setupUi(self)
class Dialog4(QtWidgets.QDialog,Ui_Dialog4): #input gross weight dialog
def __init__(self,parent=None):
super(Dialog4, self).__init__(parent)
self.setupUi(self)
class Dialog5(QtWidgets.QDialog,Ui_dialog5): #map viewer specifications dialog
def __init__(self,parent=None):
super(Dialog5, self).__init__(parent)
self.setupUi(self)
#etc
###MAIN GUI###
class mainProgram(QtWidgets.QMainWindow, Ui_MainWindow): #main window
def __init__(self, parent=None):
super(mainProgram, self).__init__(parent)
self.setupUi(self)
self.dialog = Dialog1(self)
self.dialog2 = Dialog2(self)
self.dialog3 = Dialog3(self)
self.dialog3.close()
self.dialog4 = Dialog4(self)
self.dialog5 = Dialog5(self)
self.dialog6 = Dialog6(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
splash_pix = QPixmap('loading.jpg')
splash_pix.scaled(200, 400, QtCore.Qt.KeepAspectRatio)
splash = QSplashScreen(splash_pix,Qt.WindowStaysOnTopHint)
splash.setMask(splash_pix.mask())
splash.show()
app.processEvents()
nextGui = mainProgram()
# nextGui.setWindowFlags(QtCore.Qt.FramelessWindowHint)
splash.finish(nextGui)
nextGui.showMaximized()
sys.exit(app.exec_())
Assuming that the only problem is the large number of dialogues and not that each dialogue itself has a task that consumes a lot of time so a possible option is to load each dialog every T ms so in the interim time the QSplashScreen works correctly.
# ...
###MAIN GUI###
class mainProgram(QtWidgets.QMainWindow, Ui_MainWindow):
loadFinished = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(mainProgram, self).__init__(parent)
self.setupUi(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self._T_dialogs = iter(
enumerate((Dialog1, Dialog2, Dialog3, Dialog4, Dialog5, Dialog6))
)
self._timer = QtCore.QTimer(self, timeout=self.create_dialogs, interval=100)
self._timer.start()
#QtCore.pyqtSlot()
def create_dialogs(self):
try:
i, T = next(self._T_dialogs)
w = T(self)
setattr(self, "dialog{}".format(i), w)
except StopIteration:
self._timer.stop()
self.showMaximized()
self.loadFinished.emit()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
splash_pix = QtGui.QPixmap("loading.jpg")
splash_pix.scaled(200, 400, QtCore.Qt.KeepAspectRatio)
splash = QtWidgets.QSplashScreen(splash_pix, QtCore.Qt.WindowStaysOnTopHint)
splash.setMask(splash_pix.mask())
splash.show()
nextGui = mainProgram()
nextGui.loadFinished.connect(splash.close)
sys.exit(app.exec_())

Make a modal QDialog minimize when QMainWindow minimized (using PyQt 5)

Example code I am using:
import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.open_about = False
self.openAction = QtWidgets.QAction('About', self)
self.openAction.triggered.connect(self.aboutDialog)
menuBar = self.menuBar()
fileMenu = menuBar.addMenu('&File')
fileMenu.addAction(self.openAction)
self.calendar = QtWidgets.QCalendarWidget(self)
self.setCentralWidget(self.calendar)
def about_state_upd(self, value):
self.open_about = value
def aboutDialog(self):
self._about = AboutDialog(self)
self._about.exec_()
def hideEvent(self, hideEvent):
if self.open_about == True:
self._about.setVisible(False)
def showEvent(self, showEvent):
if self.open_about == True:
if self._about.isHidden() == True:
self._about.setModal(True)
self._about.setVisible(True)
class AboutDialog(QtWidgets.QDialog):
def __init__(self, parent):
super(AboutDialog, self).__init__(parent)
self.setMinimumSize(400, 350)
self.parent().about_state_upd(True)
def closeEvent(self, closeEvent):
self.parent().about_state_upd(False)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app_window = MainWindow()
app_window.showMaximized()
sys.exit(app.exec_())
This code basically works, but seems very complicated. Is there a simpler / cleaner way to make it so that when the modal QDialog is open, if the QMainWindow is minimized, the QDialog also gets minimized too (and reverse when QMainWindow is restored)?
Code is running on KDE Neon (Kubuntu-based distro).
May be you can use this: http://korbinin.blogspot.fr/search/label/minimize%20button
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MainForm(QDialog):
def __init__(self, fn=None,parent=None):
super(MainForm, self).__init__(parent,\
flags=Qt.WindowMinimizeButtonHint|Qt.WindowMaximizeButtonHint)
Thanks to the people on the PyQt Mailing list, I managed to get a workaround for KDE. Instead of using exec_(), I am just using show() - then I use setDisabled() on QMainWindow to make dialog act in a modal fashion. Here is a (very quick and basic) example for anyone interested:
import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.openAction = QtWidgets.QAction('About', self)
self.openAction.triggered.connect(self.aboutDialog)
menuBar = self.menuBar()
fileMenu = menuBar.addMenu('&File')
fileMenu.addAction(self.openAction)
self.calendar = QtWidgets.QCalendarWidget(self)
self.setCentralWidget(self.calendar)
def aboutDialog(self):
self._about = AboutDialog(self)
self.setDisabled (True)
self._about.show()
def enableWidgets(self):
self.setDisabled(False)
class AboutDialog(QtWidgets.QDialog):
def __init__(self, parent):
super(AboutDialog, self).__init__(parent)
self.setMinimumSize(400, 350)
def closeEvent(self, parent):
self.parent().enableWidgets()
def changeEvent(self, event):
if event.type() == QtCore.QEvent.WindowStateChange:
if self.windowState() & QtCore.Qt.WindowMinimized:
self.parent().showMinimized()
else:
self.parent().showMaximized()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app_window = MainWindow()
app_window.showMaximized()
sys.exit(app.exec_())
Link to PyQt Mailing List posts.

PyQt signal slot issue with signal outside creating class

I am trying to create a toolbar that can be modified to change actions on the fly.
However signals are not being sent when I add actions from outside the class that creates the toolbar.
In the example below the new action is never triggered. Any idea on how this can be done?
import sys
from PyQt4 import QtGui
from toolbarmodifier import ToolbarModifier
class FluidToolbar(QtGui.QMainWindow):
def __init__(self):
super(FluidToolbar, self).__init__()
self.initUI()
def initUI(self):
createAction = QtGui.QAction( 'create Action', self)
createAction.triggered.connect(self.createActions)
self.toolbar = self.addToolBar('create Action')
self.toolbar.addAction(createAction)
self.setGeometry(300, 300, 300, 200)
self.show()
def createActions(self):
print(">>createActions()")
toolbarModifier = ToolbarModifier()
toolbarModifier.addAction(self)
def main():
app = QtGui.QApplication(sys.argv)
ex = FluidToolbar()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
toolbarmodifier.py
from PyQt4 import QtGui
from PyQt4.QtGui import QWidget
class ToolbarModifier(QWidget):
def __init__(self):
super(ToolbarModifier, self).__init__()
def newActionTriggered(self):
print(">>newActionTriggered()")
def addAction(self, gui):
triggerAction = QtGui.QAction( 'New action', gui)
triggerAction.triggered.connect(self.newActionTriggered)
gui.toolbar.addAction(triggerAction)
print("<<addAction()")
Not having a link back to parent was the issue. In FluidToobar modify code in createActions method to include self in call:
toolbarModifier = ToolbarModifier(self)
In ToolbarModifier change first few lines to:
class ToolbarModifier(QtCore.QObject):
def __init__(self, parent=None):
super(ToolbarModifier, self).__init__(parent)

AutoCompletion pops up on focusIn but cannot set text in QLineEdit from selection

from PyQt4.Qt import Qt, QObject,QLineEdit
from PyQt4 import QtGui, QtCore
import utils
class DirLineEdit(QLineEdit, QtCore.QObject):
"""docstring for DirLineEdit"""
def __init__(self):
super(DirLineEdit, self).__init__()
self.xmlDataObj = utils.ReadWriteCustomPathsToDisk()
self.defaultList = self.xmlDataObj.xmlData().values()
self._pathsList()
def focusInEvent(self, event):
self.completer().complete()
def _pathsList(self):
completerList = QtCore.QStringList()
for i in self.defaultList:
completerList.append(QtCore.QString(i))
lineEditCompleter = QtGui.QCompleter(completerList)
self.setCompleter(lineEditCompleter)
def __dirCompleter(self):
dirModel = QtGui.QFileSystemModel()
dirModel.setRootPath(QtCore.QDir.currentPath())
dirModel.setFilter(QtCore.QDir.AllDirs | QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
dirModel.setNameFilterDisables(0)
completer = QtGui.QCompleter(dirModel, self)
completer.setModel(dirModel)
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.setCompleter(completer)
app = QtGui.QApplication(sys.argv)
smObj = DirLineEdit()
smObj.show()
app.exec_()
The above code works except I cannot set the text from the selection I make in the AutoComplete that pops up on focus in event .. Any idea why I am not able to make set text of the selected by completer ?
The problem is that your reimplementation of focusInEvent does not call its baseclass method. You should always do this unless you are certain you want to completely override the default behaviour. In this particular case, it's also important to call the baseclass focusInEvent method before invoking the completer, because that will obviously re-take the focus.
Here's a working demo that fixes the problems in the example code:
from PyQt4 import QtGui, QtCore
class LineEdit(QtGui.QLineEdit):
def __init__(self, strings, parent):
QtGui.QLineEdit.__init__(self, parent)
self.setCompleter(QtGui.QCompleter(strings, self))
def focusInEvent(self, event):
QtGui.QLineEdit.focusInEvent(self, event)
self.completer().complete()
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
strings = 'one two three four five six seven eight'.split()
self.edit1 = LineEdit(strings, self)
self.edit2 = LineEdit(strings, self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.edit1)
layout.addWidget(self.edit2)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

PyQt window closes after launch

I'm trying to use a QPushButton to call a function that opens a new instance of QWebView. Works but as soon as the window opens it closes again.
I've read this - PyQt window closes immediately after opening but I don't understand how to reference the window to keep it open.
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtWebKit import QWebSettings
from PyQt4.QtNetwork import QNetworkAccessManager
from PyQt4.QtNetwork import *
UA_STRING = """Test Test Test"""
vidurl = ("empty")
def web1():
class YWebPage(QtWebKit.QWebPage):
def __init__(self):
super(QtWebKit.QWebPage, self).__init__()
def userAgentForUrl(self, url):
return UA_STRING
class Browser(QtGui.QMainWindow): # "Browser" window
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.resize(800,600) # Viewport size
self.centralwidget = QtGui.QWidget(self)
self.html = QtWebKit.QWebView()
def browse(self):
self.webView = QtWebKit.QWebView()
self.yPage = YWebPage()
self.webView.setPage(self.yPage)
self.webView.load(QtCore.QUrl(vidurl)) # Video URL
self.webView.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled,True) # Enables flash player
self.webView.show()
x = Browser()
# QNetworkProxy.setApplicationProxy(QNetworkProxy(QNetworkProxy.HttpProxy, "proxy.example.com", 8080)) # Proxy setting
x.browse()
def main(): # Dialog window
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
w.resize(200, 450)
w.setFixedSize(200, 350)
w.move(300, 300)
w.setWindowTitle('U-bot 0.1')
# Setup GUI
# Start Button
w.__button = QtGui.QPushButton(w)
w.__button.clicked.connect(lambda: web1())
# Text area
w.__qle = QtGui.QLineEdit(w)
w.__qle.setText ("http://")
vidurl = w.__qle.text # Get video url from user
# Images
pixmap1 = QtGui.QPixmap("ubot.png")
lbl1 = QtGui.QLabel(w)
lbl1.resize(200, 150)
lbl1.setPixmap(pixmap1)
lbl1.setScaledContents(True)
w.__button.setText('Start')
layout = QtGui.QVBoxLayout()
layout.addStretch(1)
layout.addWidget(w.__qle)
layout.addWidget(w.__button)
w.setLayout(layout)
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
app.exec_()
Create a MainWindow class that keeps a list of open Browsers, and every time when you open a browser, just add it to the list. And when a browser window closes, it will remove itself from the list, see closeEvent.
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtWebKit import QWebSettings
from PyQt4.QtNetwork import QNetworkAccessManager
from PyQt4.QtNetwork import *
UA_STRING = """Test Test Test"""
vidurl = ("empty")
class YWebPage(QtWebKit.QWebPage):
def __init__(self):
super(YWebPage, self).__init__()
def userAgentForUrl(self, url):
return UA_STRING
class Browser(QtGui.QMainWindow): # "Browser" window
def __init__(self, main, url):
QtGui.QMainWindow.__init__(self)
self.main = main
self.resize(800,600) # Viewport size
self.webView = QtWebKit.QWebView()
self.setCentralWidget(self.webView)
self.yPage = YWebPage()
self.webView.setPage(self.yPage)
self.webView.load(QtCore.QUrl(url)) # Video URL
self.webView.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True) # Enables flash player
def closeEvent(self, event):
self.main.browsers.remove(self)
super(Browser, self).closeEvent(event)
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.browsers = []
self.resize(200, 450)
self.setFixedSize(200, 350)
self.move(300, 300)
self.setWindowTitle('U-bot 0.1')
# Setup GUI
# Start Button
self.__button = QtGui.QPushButton('Start')
self.__button.clicked.connect(self.open)
# Text area
self.__qle = QtGui.QLineEdit()
self.__qle.setText("http://")
# Images
pixmap1 = QtGui.QPixmap("ubot.png")
lbl1 = QtGui.QLabel()
lbl1.resize(200, 150)
lbl1.setPixmap(pixmap1)
lbl1.setScaledContents(True)
layout = QtGui.QVBoxLayout()
layout.addStretch(1)
layout.addWidget(self.__qle)
layout.addWidget(self.__button)
self.setLayout(layout)
def open(self):
b = Browser(self, self.__qle.text())
b.show()
self.browsers.append(b)
def main():
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
To keep a reference to a QObject, you can either keep the variable in scope, or add it as the child of another QObject which variable already stays in scope.
And for QWidget, the parent should also be a QWidget, so, in your case, you'd want to make w as the parent of all your QMainWindows.
def web1(parent):
...
class Browser(QtGui.QMainWindow): # "Browser" window
def __init__(self, parent):
QtGui.QMainWindow.__init__(self, parent)
...
def main():
...
w.__button.clicked.connect(lambda: web1(w))
This also avoid maintaining manually a list of opened windows, since the object hierarchy can do that for you.
PS: The child windows are shown as toplevel windows and not inside the w window, because QMainWindow (and QDialog) has the Qt::Window flag set by default.

Categories