Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PySide6 QWidget Doesn't Automatically Create Window?
Forum Update on Monday, May 27th 2025

PySide6 QWidget Doesn't Automatically Create Window?

Scheduled Pinned Locked Moved Solved Qt for Python
qt for pythonpyside
4 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    go4tli
    wrote on last edited by
    #1

    I've been slowly working through the PySide6 tutorials at https://www.pythonguis.com/tutorials/pyside6-creating-multiple-windows/. The following code example should, when the button in MainWindow is clicked, open up a second window with a simple label: "Another Window " and a random integer. The main window and button show up, and the second window does when the button is clicked -- but the label doesn't. Why not?

    (I tried deleting the space between the percent symbol (%) and d in the string passed to QLabel, but that didn't seem to fix things. Any insight you can lend would be appreciated. Thank you for your time.)

    from PySide6.QtWidgets import (QApplication, QMainWindow, QPushButton, QLabel,
        QVBoxLayout, QWidget)
    from random import randint
    import sys
    
    
    class AnotherWindow(QWidget):
        """
        This "window" is a QWidget.  If it has no parent, it
        will appear as a free-floating window as we want.
        """
        def __init___(self):
            super().__init__()
            layout = QVBoxLayout()
            self.label = QLabel("Another Window % d" % randint(0, 100))
            layout.addWidget(self.label)
            self.setLayout(layout)
    
    
    class MainWindow(QMainWindow):
    
        def __init__(self):
            super().__init__()
            self.w = None  # No external window yet.
            self.button = QPushButton("Push for Window")
            self.button.clicked.connect(self.show_new_window)
            self.setCentralWidget(self.button)
    
        def show_new_window(self, checked):
            if self.w is None:
                self.w = AnotherWindow()
                self.w.show()
            else:
                self.w.close()  # Close window.
                self.w = None  # Discard reference, close window.
    
    
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    app.exec()
    
    SGaistS 1 Reply Last reply
    0
    • G go4tli

      I've been slowly working through the PySide6 tutorials at https://www.pythonguis.com/tutorials/pyside6-creating-multiple-windows/. The following code example should, when the button in MainWindow is clicked, open up a second window with a simple label: "Another Window " and a random integer. The main window and button show up, and the second window does when the button is clicked -- but the label doesn't. Why not?

      (I tried deleting the space between the percent symbol (%) and d in the string passed to QLabel, but that didn't seem to fix things. Any insight you can lend would be appreciated. Thank you for your time.)

      from PySide6.QtWidgets import (QApplication, QMainWindow, QPushButton, QLabel,
          QVBoxLayout, QWidget)
      from random import randint
      import sys
      
      
      class AnotherWindow(QWidget):
          """
          This "window" is a QWidget.  If it has no parent, it
          will appear as a free-floating window as we want.
          """
          def __init___(self):
              super().__init__()
              layout = QVBoxLayout()
              self.label = QLabel("Another Window % d" % randint(0, 100))
              layout.addWidget(self.label)
              self.setLayout(layout)
      
      
      class MainWindow(QMainWindow):
      
          def __init__(self):
              super().__init__()
              self.w = None  # No external window yet.
              self.button = QPushButton("Push for Window")
              self.button.clicked.connect(self.show_new_window)
              self.setCentralWidget(self.button)
      
          def show_new_window(self, checked):
              if self.w is None:
                  self.w = AnotherWindow()
                  self.w.show()
              else:
                  self.w.close()  # Close window.
                  self.w = None  # Discard reference, close window.
      
      
      app = QApplication(sys.argv)
      w = MainWindow()
      w.show()
      app.exec()
      
      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You have one underscore too many on the right side of your __init___ function.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      G 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        You have one underscore too many on the right side of your __init___ function.

        G Offline
        G Offline
        go4tli
        wrote on last edited by
        #3

        @SGaist : You're exactly right. Well, this is embarrassing. Still, thank you very much, and well spotted!

        JonBJ 1 Reply Last reply
        0
        • G go4tli

          @SGaist : You're exactly right. Well, this is embarrassing. Still, thank you very much, and well spotted!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @go4tli
          Indeed, it was very well spotted by @SGaist, I don't think I would have noticed it!

          Python could do with an override keyword to catch this, which it lacks. If you are interested for the future In Python, how do I indicate I'm overriding a method? or https://pypi.org/project/overrides/ suggest some code snippets which would catch such a mistake....

          1 Reply Last reply
          1
          • SGaistS SGaist has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved