Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Behind the Scenes
  3. Wiki Discussion
  4. How to change the label color in tab2 by clicking the push button in tab1?

How to change the label color in tab2 by clicking the push button in tab1?

Scheduled Pinned Locked Moved Unsolved Wiki Discussion
7 Posts 2 Posters 2.2k 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.
  • B Offline
    B Offline
    Beginner_to_code
    wrote on 20 Apr 2021, 19:34 last edited by Beginner_to_code
    #1

    Hi everyone,
    I am new to pyqt5, would like to create a Qwidget with 3 tabs.
    When i pressed the pushbutton in tab1 it changes the label color in tab2..
    I don't know how to toggle that..
    Somebody tell me how to do that..
    Thanks.....

    J 1 Reply Last reply 21 Apr 2021, 06:36
    0
    • B Beginner_to_code
      20 Apr 2021, 19:34

      Hi everyone,
      I am new to pyqt5, would like to create a Qwidget with 3 tabs.
      When i pressed the pushbutton in tab1 it changes the label color in tab2..
      I don't know how to toggle that..
      Somebody tell me how to do that..
      Thanks.....

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 21 Apr 2021, 06:36 last edited by
      #2

      @Beginner_to_code Create a slot and connect it to the clicked() signal of your button. In that slot change the background colour of the tab2 (you can change colour using style-sheet and setStyle()). If still not clear then please ask more specific question.

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

      B 2 Replies Last reply 21 Apr 2021, 07:10
      0
      • J jsulm
        21 Apr 2021, 06:36

        @Beginner_to_code Create a slot and connect it to the clicked() signal of your button. In that slot change the background colour of the tab2 (you can change colour using style-sheet and setStyle()). If still not clear then please ask more specific question.

        B Offline
        B Offline
        Beginner_to_code
        wrote on 21 Apr 2021, 07:10 last edited by
        #3

        @jsulm
        I don't know how to create slot?
        This is my code,

        from PyQt5 import QtGui
        from PyQt5.QtCore import Qt
        from PyQt5.QtWidgets import QApplication, QDialog, QTabWidget, QWidget, QVBoxLayout, QPushButton, QMainWindow, QLabel
        import sys
        import pyqtgraph as pg
        from pyqtgraph import PlotWidget, plot

        class Tab(QDialog):
        def init(self):
        super(Tab, self).init()

            self.setWindowTitle("Tab Widget")
            self.setGeometry(0, 0, 700, 400)
        
            vbox = QVBoxLayout()
            tabWidget = QTabWidget()
        
            tabWidget.addTab(TabSwitch(), "Switches")
            tabWidget.addTab(TabIndicator(), "Indicator")
            tabWidget.addTab(TabGraph(), "Graph")
        
            vbox.addWidget(tabWidget)
        
            self.setLayout(vbox)
        

        def clicked():
        print("clicked")

        class TabSwitch(QWidget):
        def init(self):
        super().init()

            button1 = QPushButton("On")
            button1.clicked.connect(clicked)
            button2 = QPushButton("Off")
            button3 = QPushButton("On")
            button4 = QPushButton("Off")
        
            vbox = QVBoxLayout()
        
            vbox.addWidget(button1)
            vbox.addWidget(button2)
            vbox.addWidget(button3)
            vbox.addWidget(button4)
        
            self.setLayout(vbox)
        

        class TabIndicator(QWidget):
        def init(self):
        super().init()
        self.label1 = QLabel("Indicator On", self)
        self.label1.setAlignment(Qt.AlignCenter)
        self.label1.setStyleSheet("border : 0.5px solid black;"
        "background : white;")
        # self.setStyleSheet("QLabel{color:rgb(225,22,173,255);font-size:px;font-weight:normal;font-family:Arial;}")
        self.label2 = QLabel("Indicator Off", self)
        self.label2.setAlignment(Qt.AlignCenter)
        self.label2.setStyleSheet("border : 0.5px solid black;"
        "background : white;")
        self.label3 = QLabel("Indicator On", self)
        self.label3.setAlignment(Qt.AlignCenter)
        self.label3.setStyleSheet("border : 0.5px solid black;"
        "background : white;")
        self.label4 = QLabel("Indicator Off", self)
        self.label4.setAlignment(Qt.AlignCenter)
        self.label4.setStyleSheet("border : 0.5px solid black;"
        "background : white;")
        #self.label4.setStyleSheet("QLabel::hover"
        # "{"
        # "background-color : lightgreen;"
        # "}")

            vbox = QVBoxLayout()
        
            vbox.addWidget(self.label1)
            vbox.addWidget(self.label2)
            vbox.addWidget(self.label3)
            vbox.addWidget(self.label4)
        
            self.setLayout(vbox)
        

        class TabGraph(QWidget):
        def init(self):
        super().init()

        if name == "main":
        App = QApplication(sys.argv)
        tabDialog = Tab()
        tabDialog.show()
        App.exec_()

        J 1 Reply Last reply 21 Apr 2021, 07:23
        0
        • B Beginner_to_code
          21 Apr 2021, 07:10

          @jsulm
          I don't know how to create slot?
          This is my code,

          from PyQt5 import QtGui
          from PyQt5.QtCore import Qt
          from PyQt5.QtWidgets import QApplication, QDialog, QTabWidget, QWidget, QVBoxLayout, QPushButton, QMainWindow, QLabel
          import sys
          import pyqtgraph as pg
          from pyqtgraph import PlotWidget, plot

          class Tab(QDialog):
          def init(self):
          super(Tab, self).init()

              self.setWindowTitle("Tab Widget")
              self.setGeometry(0, 0, 700, 400)
          
              vbox = QVBoxLayout()
              tabWidget = QTabWidget()
          
              tabWidget.addTab(TabSwitch(), "Switches")
              tabWidget.addTab(TabIndicator(), "Indicator")
              tabWidget.addTab(TabGraph(), "Graph")
          
              vbox.addWidget(tabWidget)
          
              self.setLayout(vbox)
          

          def clicked():
          print("clicked")

          class TabSwitch(QWidget):
          def init(self):
          super().init()

              button1 = QPushButton("On")
              button1.clicked.connect(clicked)
              button2 = QPushButton("Off")
              button3 = QPushButton("On")
              button4 = QPushButton("Off")
          
              vbox = QVBoxLayout()
          
              vbox.addWidget(button1)
              vbox.addWidget(button2)
              vbox.addWidget(button3)
              vbox.addWidget(button4)
          
              self.setLayout(vbox)
          

          class TabIndicator(QWidget):
          def init(self):
          super().init()
          self.label1 = QLabel("Indicator On", self)
          self.label1.setAlignment(Qt.AlignCenter)
          self.label1.setStyleSheet("border : 0.5px solid black;"
          "background : white;")
          # self.setStyleSheet("QLabel{color:rgb(225,22,173,255);font-size:px;font-weight:normal;font-family:Arial;}")
          self.label2 = QLabel("Indicator Off", self)
          self.label2.setAlignment(Qt.AlignCenter)
          self.label2.setStyleSheet("border : 0.5px solid black;"
          "background : white;")
          self.label3 = QLabel("Indicator On", self)
          self.label3.setAlignment(Qt.AlignCenter)
          self.label3.setStyleSheet("border : 0.5px solid black;"
          "background : white;")
          self.label4 = QLabel("Indicator Off", self)
          self.label4.setAlignment(Qt.AlignCenter)
          self.label4.setStyleSheet("border : 0.5px solid black;"
          "background : white;")
          #self.label4.setStyleSheet("QLabel::hover"
          # "{"
          # "background-color : lightgreen;"
          # "}")

              vbox = QVBoxLayout()
          
              vbox.addWidget(self.label1)
              vbox.addWidget(self.label2)
              vbox.addWidget(self.label3)
              vbox.addWidget(self.label4)
          
              self.setLayout(vbox)
          

          class TabGraph(QWidget):
          def init(self):
          super().init()

          if name == "main":
          App = QApplication(sys.argv)
          tabDialog = Tab()
          tabDialog.show()
          App.exec_()

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 21 Apr 2021, 07:23 last edited by
          #4

          @Beginner_to_code said in How to change the label color in tab2 by clicking the push button in tab1?:

          I don't know how to create slot?

          Then please learn - this is an absolute basic feature in Qt. https://doc.qt.io/qt-5/signalsandslots.html https://www.tutorialspoint.com/pyqt/pyqt_signals_and_slots.htm

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

          1 Reply Last reply
          2
          • J jsulm
            21 Apr 2021, 06:36

            @Beginner_to_code Create a slot and connect it to the clicked() signal of your button. In that slot change the background colour of the tab2 (you can change colour using style-sheet and setStyle()). If still not clear then please ask more specific question.

            B Offline
            B Offline
            Beginner_to_code
            wrote on 21 Apr 2021, 07:36 last edited by
            #5

            @jsulm
            Created a slot and connected it to the clicked signal of that button..
            But how should i change the background color of the label in tab2?

            J 1 Reply Last reply 21 Apr 2021, 07:39
            0
            • B Beginner_to_code
              21 Apr 2021, 07:36

              @jsulm
              Created a slot and connected it to the clicked signal of that button..
              But how should i change the background color of the label in tab2?

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 21 Apr 2021, 07:39 last edited by
              #6

              @Beginner_to_code said in How to change the label color in tab2 by clicking the push button in tab1?:

              But how should i change the background color of the label in tab2?

              I already suggested to use style-sheet: https://doc.qt.io/qt-5/stylesheet-examples.html

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

              B 1 Reply Last reply 21 Apr 2021, 07:48
              2
              • J jsulm
                21 Apr 2021, 07:39

                @Beginner_to_code said in How to change the label color in tab2 by clicking the push button in tab1?:

                But how should i change the background color of the label in tab2?

                I already suggested to use style-sheet: https://doc.qt.io/qt-5/stylesheet-examples.html

                B Offline
                B Offline
                Beginner_to_code
                wrote on 21 Apr 2021, 07:48 last edited by
                #7

                @jsulm
                ok, thanks...

                1 Reply Last reply
                0

                7/7

                21 Apr 2021, 07:48

                • Login

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