PyQt window closes after launch

metalayer

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PyQt window closes after opening

From Dev

PyQt window crashes after launch

From Dev

PyQt QWidget window closes immediately upon showing?

From Dev

PyQt5 dialog closes main window

From Dev

Google PlacePicker Closes Immediately After Launch

From Dev

Android Place Picker closes immediately after launch

From Dev

Bootstrap window closes after alert statement

From Dev

Firebase popup window closes after calling login

From Dev

Main Window closes after execvp() call

From Dev

Firebase popup window closes after calling login

From Dev

.exe closes immediately after launch when double clicked

From Dev

launch exe with params, but program closes instantly after opening?

From Dev

.exe closes immediately after launch when double clicked

From Dev

Removing the :focus state from a link after modal window closes

From Dev

Tkinter window closes automatically after Python program has run in PyCharm

From Dev

go to previous window automatically after printing dialog closes

From Dev

Tkinter window closes automatically after Python program has run in PyCharm

From Dev

JUnit stops executing after click closes the popup window

From Dev

PyQt thread still running after window closed

From Dev

PyQt4 to launch system tray icon immediately after login

From Dev

Win Form exe closes immediately after launch, deploying a win form application using setup project

From Dev

Window closes too fast

From Dev

SFML window immediately closes

From Java

QThread closes whenever QFileDialog Called After Migrating from PyQt5 to Pyside2

From Dev

How can I return information after my main window closes in Qt?

From Dev

How can I return information after my main window closes in Qt?

From Dev

How do I execute more code after closing a PyQt window?

From Dev

App closes after posting 'č','š' and 'ž'

From Dev

My code closes the workbook but not the window

Related Related

  1. 1

    PyQt window closes after opening

  2. 2

    PyQt window crashes after launch

  3. 3

    PyQt QWidget window closes immediately upon showing?

  4. 4

    PyQt5 dialog closes main window

  5. 5

    Google PlacePicker Closes Immediately After Launch

  6. 6

    Android Place Picker closes immediately after launch

  7. 7

    Bootstrap window closes after alert statement

  8. 8

    Firebase popup window closes after calling login

  9. 9

    Main Window closes after execvp() call

  10. 10

    Firebase popup window closes after calling login

  11. 11

    .exe closes immediately after launch when double clicked

  12. 12

    launch exe with params, but program closes instantly after opening?

  13. 13

    .exe closes immediately after launch when double clicked

  14. 14

    Removing the :focus state from a link after modal window closes

  15. 15

    Tkinter window closes automatically after Python program has run in PyCharm

  16. 16

    go to previous window automatically after printing dialog closes

  17. 17

    Tkinter window closes automatically after Python program has run in PyCharm

  18. 18

    JUnit stops executing after click closes the popup window

  19. 19

    PyQt thread still running after window closed

  20. 20

    PyQt4 to launch system tray icon immediately after login

  21. 21

    Win Form exe closes immediately after launch, deploying a win form application using setup project

  22. 22

    Window closes too fast

  23. 23

    SFML window immediately closes

  24. 24

    QThread closes whenever QFileDialog Called After Migrating from PyQt5 to Pyside2

  25. 25

    How can I return information after my main window closes in Qt?

  26. 26

    How can I return information after my main window closes in Qt?

  27. 27

    How do I execute more code after closing a PyQt window?

  28. 28

    App closes after posting 'č','š' and 'ž'

  29. 29

    My code closes the workbook but not the window

HotTag

Archive