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. Pop-up with colors and data
Forum Updated to NodeBB v4.3 + New Features

Pop-up with colors and data

Scheduled Pinned Locked Moved Solved Qt for Python
35 Posts 4 Posters 5.5k 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.
  • J Offline
    J Offline
    john_hobbyist
    wrote on last edited by john_hobbyist
    #9

    This is my try, by modifying the above code and this example: https://www.tutorialspoint.com/pyqt/pyqt_qgridlayout_class.htm ! Where do I put this line of code:

    self.label.setStyleSheet("border: 1px solid black; background-color: red")
    

    in order to color the boxes? I tried also with self. etc nothing!

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    def window():
        app = QApplication(sys.argv)
        win = QWidget()
        grid=QGridLayout()
        
        grid.addWidget(QLabel("Yellow"), 1,1)
        grid.addWidget(QPushButton("B"+str(1)+str(2)),1,2)
        grid.addWidget(QLabel("Red"), 2,1)
        grid.addWidget(QPushButton("B"+str(2)+str(2)),2,2)
        grid.addWidget(QLabel("Blue"), 3,1)
        grid.addWidget(QPushButton("B"+str(3)+str(2)),3,2)
        
        win.setLayout(grid)
        win.setGeometry(100,100,200,100)
        win.setWindowTitle("PyQt")
        win.show()
        
        sys.exit(app.exec_())
         
    if __name__ == '__main__':
     window()
    
    eyllanescE 1 Reply Last reply
    0
    • J john_hobbyist

      This is my try, by modifying the above code and this example: https://www.tutorialspoint.com/pyqt/pyqt_qgridlayout_class.htm ! Where do I put this line of code:

      self.label.setStyleSheet("border: 1px solid black; background-color: red")
      

      in order to color the boxes? I tried also with self. etc nothing!

      import sys
      from PyQt5.QtCore import *
      from PyQt5.QtWidgets import *
      def window():
          app = QApplication(sys.argv)
          win = QWidget()
          grid=QGridLayout()
          
          grid.addWidget(QLabel("Yellow"), 1,1)
          grid.addWidget(QPushButton("B"+str(1)+str(2)),1,2)
          grid.addWidget(QLabel("Red"), 2,1)
          grid.addWidget(QPushButton("B"+str(2)+str(2)),2,2)
          grid.addWidget(QLabel("Blue"), 3,1)
          grid.addWidget(QPushButton("B"+str(3)+str(2)),3,2)
          
          win.setLayout(grid)
          win.setGeometry(100,100,200,100)
          win.setWindowTitle("PyQt")
          win.show()
          
          sys.exit(app.exec_())
           
      if __name__ == '__main__':
       window()
      
      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #10

      @john_hobbyist Change:

      grid.addWidget(QLabel("Yellow"), 1,1)
      

      to

      label_yellow = QLabel("Yellow")
      label_yellow.setStyleSheet(...)
      grid.addWidget(label_yellow, 1,1)
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      1
      • J Offline
        J Offline
        john_hobbyist
        wrote on last edited by
        #11

        I made the above code a method and I call it from PyQt5 GUI. The result is this:

        QWidget::setLayout: Attempting to set QLayout "" on TestCode"", which already has a layout
        QCoreApplication::exec: The event loop is already running
        Segmentation fault (core dumped)
        

        Any ideas what is wrong?

        The method is this:

        def PrintGridLayout (self):
         ....
        
        jsulmJ 1 Reply Last reply
        0
        • J john_hobbyist

          I made the above code a method and I call it from PyQt5 GUI. The result is this:

          QWidget::setLayout: Attempting to set QLayout "" on TestCode"", which already has a layout
          QCoreApplication::exec: The event loop is already running
          Segmentation fault (core dumped)
          

          Any ideas what is wrong?

          The method is this:

          def PrintGridLayout (self):
           ....
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #12

          @john_hobbyist said in Pop-up with colors and data:

          def PrintGridLayout (self):
          ....

          Why don't you post whole code instead of ...?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • J Offline
            J Offline
            john_hobbyist
            wrote on last edited by
            #13

            @john_hobbyist said in Pop-up with colors and data:

            def PrintGridLayout (self):

            def PrintGridLayout (self):
                def PrintGridLayout (self):
                app = QApplication(sys.argv)
                win = QWidget()
                grid=QGridLayout()
                
                grid.addWidget(QLabel("Yellow"), 1,1)
                grid.addWidget(QPushButton("B"+str(1)+str(2)),1,2)
                grid.addWidget(QLabel("Red"), 2,1)
                grid.addWidget(QPushButton("B"+str(2)+str(2)),2,2)
                grid.addWidget(QLabel("Blue"), 3,1)
                grid.addWidget(QPushButton("B"+str(3)+str(2)),3,2)
                
                win.setLayout(grid)
                win.setGeometry(100,100,200,100)
                win.setWindowTitle("PyQt")
                win.show()
                
                sys.exit(app.exec_())
            
            jsulmJ 1 Reply Last reply
            0
            • J john_hobbyist

              @john_hobbyist said in Pop-up with colors and data:

              def PrintGridLayout (self):

              def PrintGridLayout (self):
                  def PrintGridLayout (self):
                  app = QApplication(sys.argv)
                  win = QWidget()
                  grid=QGridLayout()
                  
                  grid.addWidget(QLabel("Yellow"), 1,1)
                  grid.addWidget(QPushButton("B"+str(1)+str(2)),1,2)
                  grid.addWidget(QLabel("Red"), 2,1)
                  grid.addWidget(QPushButton("B"+str(2)+str(2)),2,2)
                  grid.addWidget(QLabel("Blue"), 3,1)
                  grid.addWidget(QPushButton("B"+str(3)+str(2)),3,2)
                  
                  win.setLayout(grid)
                  win.setGeometry(100,100,200,100)
                  win.setWindowTitle("PyQt")
                  win.show()
                  
                  sys.exit(app.exec_())
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #14

              @john_hobbyist said in Pop-up with colors and data:

              def PrintGridLayout (self):
              def PrintGridLayout (self):

              Why do you have nested PrintGridLayout?
              Why do you have "app = QApplication(sys.argv)" and "sys.exit(app.exec_())"? As one of the error messages tells you you already have event loop running!

              Regarding first error: please read https://doc.qt.io/qt-5/qwidget.html#setLayout

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply
              2
              • jsulmJ jsulm

                @john_hobbyist said in Pop-up with colors and data:

                def PrintGridLayout (self):
                def PrintGridLayout (self):

                Why do you have nested PrintGridLayout?
                Why do you have "app = QApplication(sys.argv)" and "sys.exit(app.exec_())"? As one of the error messages tells you you already have event loop running!

                Regarding first error: please read https://doc.qt.io/qt-5/qwidget.html#setLayout

                J Offline
                J Offline
                john_hobbyist
                wrote on last edited by
                #15

                @jsulm Yes the first one is a copy paste error from Kubuntu to here...

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  john_hobbyist
                  wrote on last edited by john_hobbyist
                  #16

                  @jsulm said in Pop-up with colors and data:

                  app = QApplication(sys.argv)" and "sys.exit(app.exec_())

                  Thanks....I deleted these:

                  app = QApplication(sys.argv)
                  

                  and

                  sys.exit(app.exec_())
                  

                  and now I get only this error:

                  QWidget::setLayout: Attempting to set QLayout "" on TestCode"", which already has a layout
                  

                  which I try to figure out how to solve it... because it shows only this error and nothing else is depicted in the PyQt5 GUI...

                  By the way, the above method I call it like this:

                      test = QAction("Testing Layout", self) 
                      file.addAction(test) # here
                      test.triggered.connect(self.PrintGridLayout)
                  

                  Update 1: However, the rest QAction choices (buttons) work well..

                  Update 2: I put everywhere inside the method code print commands... everything runs inside the method however nothing is displayed in the GUI...

                  jsulmJ 1 Reply Last reply
                  0
                  • J john_hobbyist

                    @jsulm said in Pop-up with colors and data:

                    app = QApplication(sys.argv)" and "sys.exit(app.exec_())

                    Thanks....I deleted these:

                    app = QApplication(sys.argv)
                    

                    and

                    sys.exit(app.exec_())
                    

                    and now I get only this error:

                    QWidget::setLayout: Attempting to set QLayout "" on TestCode"", which already has a layout
                    

                    which I try to figure out how to solve it... because it shows only this error and nothing else is depicted in the PyQt5 GUI...

                    By the way, the above method I call it like this:

                        test = QAction("Testing Layout", self) 
                        file.addAction(test) # here
                        test.triggered.connect(self.PrintGridLayout)
                    

                    Update 1: However, the rest QAction choices (buttons) work well..

                    Update 2: I put everywhere inside the method code print commands... everything runs inside the method however nothing is displayed in the GUI...

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #17

                    @john_hobbyist said in Pop-up with colors and data:

                    which I try to figure out how to solve it

                    That's why I suggested to read https://doc.qt.io/qt-5/qwidget.html#setLayout
                    Especially this part:
                    "If there already is a layout manager installed on this widget, QWidget won't let you install another. You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout."

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • J Offline
                      J Offline
                      john_hobbyist
                      wrote on last edited by john_hobbyist
                      #18

                      @jsulm said in Pop-up with colors and data:

                      If there already is a layout manager installed on this widget

                      When to code runs, it opens a main window/GUI, and when the user chooses from the menu (QAction) , he can open an auxiliary window with some information. What should I delete? Both of them are required...

                      Can you give me an example code, in order to understand?

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        john_hobbyist
                        wrote on last edited by john_hobbyist
                        #19

                        As I checked from here: https://doc.qt.io/qt-5/layout.html under paragraph: "Qt's Layout Classes":

                        I use these classes:

                        QHBoxLayout
                        

                        and

                        QSizePolicy
                        

                        under this method (and after that is the method I described previously):

                        class Thing(QMainWindow):
                           def __init__(self, parent = None):
                              . . . 
                              (here are the QHBoxLayout and QSizePolicy used)
                        	  . . . 
                        
                           def PrintGridLayout (self):
                              def PrintGridLayout (self):
                              win = QWidget()
                              grid=QGridLayout()
                            
                              grid.addWidget(QLabel("Yellow"), 1,1)
                              grid.addWidget(QPushButton("B"+str(1)+str(2)),1,2)
                              grid.addWidget(QLabel("Red"), 2,1)
                              grid.addWidget(QPushButton("B"+str(2)+str(2)),2,2)
                              grid.addWidget(QLabel("Blue"), 3,1)
                              grid.addWidget(QPushButton("B"+str(3)+str(2)),3,2)
                            
                              win.setLayout(grid)
                              win.setGeometry(100,100,200,100)
                              win.setWindowTitle("PyQt")
                              win.show()
                        

                        So how do I delete the existing layout manager? And especially since they are on a different class??

                        JonBJ 1 Reply Last reply
                        0
                        • J john_hobbyist

                          As I checked from here: https://doc.qt.io/qt-5/layout.html under paragraph: "Qt's Layout Classes":

                          I use these classes:

                          QHBoxLayout
                          

                          and

                          QSizePolicy
                          

                          under this method (and after that is the method I described previously):

                          class Thing(QMainWindow):
                             def __init__(self, parent = None):
                                . . . 
                                (here are the QHBoxLayout and QSizePolicy used)
                          	  . . . 
                          
                             def PrintGridLayout (self):
                                def PrintGridLayout (self):
                                win = QWidget()
                                grid=QGridLayout()
                              
                                grid.addWidget(QLabel("Yellow"), 1,1)
                                grid.addWidget(QPushButton("B"+str(1)+str(2)),1,2)
                                grid.addWidget(QLabel("Red"), 2,1)
                                grid.addWidget(QPushButton("B"+str(2)+str(2)),2,2)
                                grid.addWidget(QLabel("Blue"), 3,1)
                                grid.addWidget(QPushButton("B"+str(3)+str(2)),3,2)
                              
                                win.setLayout(grid)
                                win.setGeometry(100,100,200,100)
                                win.setWindowTitle("PyQt")
                                win.show()
                          

                          So how do I delete the existing layout manager? And especially since they are on a different class??

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

                          @john_hobbyist

                          win = QWidget()
                          ...
                          win.setLayout(grid)
                          

                          I would not expect this code to generate

                          QWidget::setLayout: Attempting to set QLayout "" on TestCode"", which already has a layout
                          

                          since a blank QWidget does not already have a layout. OTOH, I would expect mainWindow.setLayout() to do so, because QMainWindow does start with a layout. So where exactly are you getting the error from? (I believe Python reports the source code line number in its full message?)

                          When you copy & paste your code here, could you please take the time to do so correctly, as incorrect code pasting wastes peoples' time trying to understand. I very much doubt your code actually has

                             def PrintGridLayout (self):
                                def PrintGridLayout (self):
                          

                          as you show.

                          J 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @john_hobbyist

                            win = QWidget()
                            ...
                            win.setLayout(grid)
                            

                            I would not expect this code to generate

                            QWidget::setLayout: Attempting to set QLayout "" on TestCode"", which already has a layout
                            

                            since a blank QWidget does not already have a layout. OTOH, I would expect mainWindow.setLayout() to do so, because QMainWindow does start with a layout. So where exactly are you getting the error from? (I believe Python reports the source code line number in its full message?)

                            When you copy & paste your code here, could you please take the time to do so correctly, as incorrect code pasting wastes peoples' time trying to understand. I very much doubt your code actually has

                               def PrintGridLayout (self):
                                  def PrintGridLayout (self):
                            

                            as you show.

                            J Offline
                            J Offline
                            john_hobbyist
                            wrote on last edited by john_hobbyist
                            #21

                            @JonB Ok, no problem with this message. The code worked for many days...! Ok, I will try to be more careful with copying.. My concern now is how do I delete the existing layout manager?

                            JonBJ 1 Reply Last reply
                            0
                            • J john_hobbyist

                              @JonB Ok, no problem with this message. The code worked for many days...! Ok, I will try to be more careful with copying.. My concern now is how do I delete the existing layout manager?

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

                              @john_hobbyist said in Pop-up with colors and data:

                              My concern now is how do I delete the existing layout manager?

                              Why do you want to delete if you do not get an error message? If you do get an error message, I asked you where it came from? I am totally uncertain what situation you are in, your posts are unclear.

                              J 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @john_hobbyist said in Pop-up with colors and data:

                                My concern now is how do I delete the existing layout manager?

                                Why do you want to delete if you do not get an error message? If you do get an error message, I asked you where it came from? I am totally uncertain what situation you are in, your posts are unclear.

                                J Offline
                                J Offline
                                john_hobbyist
                                wrote on last edited by john_hobbyist
                                #23

                                @JonB Ι re-post:

                                As I checked from here: https://doc.qt.io/qt-5/layout.html under paragraph: "Qt's Layout Classes":

                                I use these classes:

                                QHBoxLayout
                                

                                and

                                QSizePolicy
                                

                                under this method (and after that is the method I described previously):

                                class Thing(QMainWindow):
                                   def __init__(self, parent = None):
                                      . . . 
                                      (here are the QHBoxLayout and QSizePolicy used)
                                	  . . . 
                                
                                   def PrintGridLayout (self):
                                      win = QWidget()
                                      grid=QGridLayout()
                                    
                                      grid.addWidget(QLabel("Yellow"), 1,1)
                                      grid.addWidget(QPushButton("B"+str(1)+str(2)),1,2)
                                      grid.addWidget(QLabel("Red"), 2,1)
                                      grid.addWidget(QPushButton("B"+str(2)+str(2)),2,2)
                                      grid.addWidget(QLabel("Blue"), 3,1)
                                      grid.addWidget(QPushButton("B"+str(3)+str(2)),3,2)
                                    
                                      win.setLayout(grid)
                                      win.setGeometry(100,100,200,100)
                                      win.setWindowTitle("PyQt")
                                      win.show()
                                

                                So how do I delete the existing layout manager? And especially since they are on a different class??

                                JonBJ 1 Reply Last reply
                                0
                                • J john_hobbyist

                                  @JonB Ι re-post:

                                  As I checked from here: https://doc.qt.io/qt-5/layout.html under paragraph: "Qt's Layout Classes":

                                  I use these classes:

                                  QHBoxLayout
                                  

                                  and

                                  QSizePolicy
                                  

                                  under this method (and after that is the method I described previously):

                                  class Thing(QMainWindow):
                                     def __init__(self, parent = None):
                                        . . . 
                                        (here are the QHBoxLayout and QSizePolicy used)
                                  	  . . . 
                                  
                                     def PrintGridLayout (self):
                                        win = QWidget()
                                        grid=QGridLayout()
                                      
                                        grid.addWidget(QLabel("Yellow"), 1,1)
                                        grid.addWidget(QPushButton("B"+str(1)+str(2)),1,2)
                                        grid.addWidget(QLabel("Red"), 2,1)
                                        grid.addWidget(QPushButton("B"+str(2)+str(2)),2,2)
                                        grid.addWidget(QLabel("Blue"), 3,1)
                                        grid.addWidget(QPushButton("B"+str(3)+str(2)),3,2)
                                      
                                        win.setLayout(grid)
                                        win.setGeometry(100,100,200,100)
                                        win.setWindowTitle("PyQt")
                                        win.show()
                                  

                                  So how do I delete the existing layout manager? And especially since they are on a different class??

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

                                  @john_hobbyist
                                  If you do not get an error message you have nothing to do. If you do get an error message then show it, and find the line which caused in it which I think Python will show in the message. This is the third time I have said this.

                                  J 1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @john_hobbyist
                                    If you do not get an error message you have nothing to do. If you do get an error message then show it, and find the line which caused in it which I think Python will show in the message. This is the third time I have said this.

                                    J Offline
                                    J Offline
                                    john_hobbyist
                                    wrote on last edited by
                                    #25

                                    @JonB No error, the method above runs (I checked it with print commands among the code). However, no GUI is displayed with the GridLayout, Buttons e.t.c.

                                    JonBJ 1 Reply Last reply
                                    0
                                    • J john_hobbyist

                                      @JonB No error, the method above runs (I checked it with print commands among the code). However, no GUI is displayed with the GridLayout, Buttons e.t.c.

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

                                      @john_hobbyist said in Pop-up with colors and data:

                                      However, no GUI is displayed with the GridLayout, Buttons e.t.c.

                                      Could you please tell me: does this mean that an "empty" widget/window is displayed (presumably with title bar PyQt) or does this mean no widget/window at all is shown?

                                      J 1 Reply Last reply
                                      0
                                      • JonBJ JonB

                                        @john_hobbyist said in Pop-up with colors and data:

                                        However, no GUI is displayed with the GridLayout, Buttons e.t.c.

                                        Could you please tell me: does this mean that an "empty" widget/window is displayed (presumably with title bar PyQt) or does this mean no widget/window at all is shown?

                                        J Offline
                                        J Offline
                                        john_hobbyist
                                        wrote on last edited by
                                        #27

                                        @JonB no widget/window at all is shown for this specific method...

                                        JonBJ 1 Reply Last reply
                                        0
                                        • J john_hobbyist

                                          @JonB no widget/window at all is shown for this specific method...

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

                                          @john_hobbyist
                                          So two possibilities occur to me:

                                          • You create a widget (win = QWidget()) but never "attach" it anywhere to your main window. Where do you add it to that?

                                          • Your win = QWidget() is a local reference which goes out of scope at the end of PrintGridLayout in Python, doesn't it? So won't it get destroyed, and hence no widget is shown?

                                          So far as I can tell from what you are saying, you might as well remove all your QGridLayout code and you would see the same thing. So it has nothing to do with using the layout, it just isn't showing the widget?

                                          J 1 Reply Last reply
                                          0

                                          • Login

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