0

If I click the button (Pushme) then it should as test print Hello World! but it doesn't work. Any tips?

    from PyQt5.QtWidgets import QMainWindow, QApplication
    from PyQt5 import uic
    from PyQt5.QtGui import QIcon

    class Main(QMainWindow):

        def __init__(self):
            super(Main, self).__init__()
            uic.loadUi("Youtubedownload.ui", self)
            self.show()
            self.setWindowTitle("YoutubeDownloader")
            self.setWindowIcon(QIcon("download.ico"))

            self.Convertbutton.clicked.connect(self.Open_SecondWindow)

        def Open_SecondWindow(self):
            secondwindow = SecondWindow()
            secondwindow.Youtube_text.setText(self.Youtubelink.text())
            secondwindow.__init__()
            self.close()

    class SecondWindow(QMainWindow):
        def __init__(self):
            super(SecondWindow, self).__init__()
            uic.loadUi("Youtubedownload_loading.ui", self)
            self.show()
            self.setWindowTitle("YoutubeDownloader")
            self.setWindowIcon(QIcon("download.ico"))

            self.Pushme.clicked.connect(self.Convert)


        def Convert(self):
            print("Hello World!")

    def main():
        app = QApplication([])
        window = Main()
        app.exec_()

    if __name__ == '__main__':
        main()
musicamante
  • 41,230
  • 6
  • 33
  • 58
Kevin
  • 3
  • 2
  • What is `Pushme`? It's not defined anywhere. – mkrieger1 Aug 18 '22 at 15:30
  • Thats my button to start the converting but that does not work either so I tried with print but that doesn't work... – Kevin Aug 18 '22 at 15:53
  • 1
    According to your code, the second window shouldn't even appear (or will be closed almost instantly) since `secondwindow` is local an gets garbage collected. Also, explicitly calling `__init__` is not only unnecessary, but also **wrong**. And only classes and constants should have capitalized names, not variables, attributes or functions (see the official [Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/)). – musicamante Aug 18 '22 at 15:59
  • Ooh can you fix it maybe that it will work? – Kevin Aug 18 '22 at 16:05
  • normally you would use `self.secondwindow` instead of `secondwindow` to fix it but it seems you close first window and it may destroy this variable - so you may need to use `global secondwindow` inside `Open_SecondWindow` – furas Aug 19 '22 at 10:19

1 Answers1

0

You assign window to local variable so garbage collector removes window at once (when it finishs function Open_SecondWindow) and you simply can't see it.

You have to use self.secondwindow instead of secondwindow` to fix it.

BTW: you shouldn't run manually .__init__()


Minimal working code - so everyone can copy and test it

from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QLineEdit
#from PyQt5 import uic
#from PyQt5.QtGui import QIcon

class Main(QMainWindow):

    def __init__(self):
        super().__init__()

        self.convert_button = QPushButton('Second Window', self)   # PEP8: `lower_case_names` for variables
        self.convert_button.move(10, 10)

        self.youtube_link = QLineEdit("???", self)
        self.youtube_link.move(10, 50)

        self.show()
        
        self.setWindowTitle("YoutubeDownloader")
        #self.setWindowIcon(QIcon("download.ico"))

        self.convert_button.clicked.connect(self.open_second_window)

    def open_second_window(self):  # PEP8: `lower_case_names` for functions
        self.second_window = SecondWindow()
        self.second_window.youtube_text.setText(self.youtube_link.text())
        self.close()

class SecondWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.pushme = QPushButton('Push Me', self)   # PEP8: `lower_case_names` for variables
        self.pushme.move(10, 10)
        
        self.youtube_text = QLabel("?", self)
        self.youtube_text.move(10, 50)
        
        self.show()
        
        self.setWindowTitle("YoutubeDownloader")
        #self.setWindowIcon(QIcon("download.ico"))

        self.pushme.clicked.connect(self.convert)

    def convert(self):  # PEP8: `lower_case_names` for functions
        print("Hello World!")

def main():
    app = QApplication([])
    window = Main()
    app.exec_()

if __name__ == '__main__':
    main()

PEP 8 -- Style Guide for Python Code

furas
  • 134,197
  • 12
  • 106
  • 148