在 Pyqt5 中悬停时如何更改 QLabel 的字体大小?
How to change the font-size of QLabel When hovering In Pyqt5?
为自定义标签使用以下样式表代码。悬停时,背景色:绿色;颜色为白色;完美运行,但是 我的问题: 字体大小:27px;字体粗细:700;将无法按预期工作。 (没有变化,字体大小和字体粗细)。如何解决?
MyLabel[button63="1"]
{
text-align:left; font-size:17px; font-family: Calibri;
color:black; background-color:rgb(212,185,150); border-style: flat;
}
MyLabel[button63="1"]:hover
{
font-size: 27px; font-weight:700; background-color: green; color: white;
}
第一个节目
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSignal,Qt
import assist_stylesheet_002
class MyLabel(QLabel):
leftclicked = pyqtSignal()
def mousePressEvent(self, ev):
if ev.button() == Qt.LeftButton:
self.leftclicked.emit()
QLabel.mousePressEvent(self, ev)
#-----------------------------------------------------------------------------
class AssistMain(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle(" Css Style Sheet")
self.setGeometry(100,100,600,600)
self.lbl_country = MyLabel("Country")
self.lbl_country.setProperty("type",'1')
self.lbl_country.setFixedSize(100,30)
self.lbl_state = MyLabel("State")
self.lbl_state.setProperty("type",'1')
self.lbl_state.setFixedSize(100,30)
self.lbl_town = MyLabel("Town")
self.lbl_town.setProperty("type",'1')
self.lbl_town.setFixedSize(100,30)
self.vbox = QVBoxLayout()
self.vbox.setSpacing(10)
self.vbox.addWidget(self.lbl_country)
self.vbox.addWidget(self.lbl_state)
self.vbox.addWidget(self.lbl_town)
self.vbox.addStretch()
# self.setLayout(self.vbox)
widget = QWidget()
widget.setLayout(self.vbox)
self.setCentralWidget(widget)
def main():
app = QApplication(sys.argv)
mainwindow = AssistMain()
qApp.setStyleSheet(assist_stylesheet_002.style_toplayout())
app.setStyle("fusion")
mainwindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
CSS 样式 Sheet
def style_toplayout():
return """
MyLabel[type="1"]
{
text-align:left; font-size:17px; font-family: Calibri;
color:black; background-color:rgb(212,185,150); border-style: flat;
}
MyLabel[type="1"]:hover
{
font-size: 27px; font-weight:700; background-color: green; color: white;
}
"""
似乎无法使用 QSS 动态更改字体,因此一个可能的解决方案是覆盖 enterEvent 和 leaveEvent 方法以使用 QFont 更改字体:
class MyLabel(QLabel):
leftclicked = pyqtSignal()
def mousePressEvent(self, ev):
if ev.button() == Qt.LeftButton:
self.leftclicked.emit()
QLabel.mousePressEvent(self, ev)
def enterEvent(self, event):
super().enterEvent(event)
font = QFont("Calibri")
font.setPointSize(27)
self.setFont(font)
def leaveEvent(self, event):
super().leaveEvent(event)
font = QFont("Calibri")
font.setPointSize(17)
self.setFont(font)
为自定义标签使用以下样式表代码。悬停时,背景色:绿色;颜色为白色;完美运行,但是 我的问题: 字体大小:27px;字体粗细:700;将无法按预期工作。 (没有变化,字体大小和字体粗细)。如何解决?
MyLabel[button63="1"]
{
text-align:left; font-size:17px; font-family: Calibri;
color:black; background-color:rgb(212,185,150); border-style: flat;
}
MyLabel[button63="1"]:hover
{
font-size: 27px; font-weight:700; background-color: green; color: white;
}
第一个节目
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSignal,Qt
import assist_stylesheet_002
class MyLabel(QLabel):
leftclicked = pyqtSignal()
def mousePressEvent(self, ev):
if ev.button() == Qt.LeftButton:
self.leftclicked.emit()
QLabel.mousePressEvent(self, ev)
#-----------------------------------------------------------------------------
class AssistMain(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle(" Css Style Sheet")
self.setGeometry(100,100,600,600)
self.lbl_country = MyLabel("Country")
self.lbl_country.setProperty("type",'1')
self.lbl_country.setFixedSize(100,30)
self.lbl_state = MyLabel("State")
self.lbl_state.setProperty("type",'1')
self.lbl_state.setFixedSize(100,30)
self.lbl_town = MyLabel("Town")
self.lbl_town.setProperty("type",'1')
self.lbl_town.setFixedSize(100,30)
self.vbox = QVBoxLayout()
self.vbox.setSpacing(10)
self.vbox.addWidget(self.lbl_country)
self.vbox.addWidget(self.lbl_state)
self.vbox.addWidget(self.lbl_town)
self.vbox.addStretch()
# self.setLayout(self.vbox)
widget = QWidget()
widget.setLayout(self.vbox)
self.setCentralWidget(widget)
def main():
app = QApplication(sys.argv)
mainwindow = AssistMain()
qApp.setStyleSheet(assist_stylesheet_002.style_toplayout())
app.setStyle("fusion")
mainwindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
CSS 样式 Sheet
def style_toplayout():
return """
MyLabel[type="1"]
{
text-align:left; font-size:17px; font-family: Calibri;
color:black; background-color:rgb(212,185,150); border-style: flat;
}
MyLabel[type="1"]:hover
{
font-size: 27px; font-weight:700; background-color: green; color: white;
}
"""
似乎无法使用 QSS 动态更改字体,因此一个可能的解决方案是覆盖 enterEvent 和 leaveEvent 方法以使用 QFont 更改字体:
class MyLabel(QLabel):
leftclicked = pyqtSignal()
def mousePressEvent(self, ev):
if ev.button() == Qt.LeftButton:
self.leftclicked.emit()
QLabel.mousePressEvent(self, ev)
def enterEvent(self, event):
super().enterEvent(event)
font = QFont("Calibri")
font.setPointSize(27)
self.setFont(font)
def leaveEvent(self, event):
super().leaveEvent(event)
font = QFont("Calibri")
font.setPointSize(17)
self.setFont(font)