How to change the label color in tab2 by clicking the push button in tab1?
-
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..... -
@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.
-
@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, plotclass 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_() -
@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
-
@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