【发布时间】:2021-07-05 23:36:16
【问题描述】:
当鼠标悬停在按钮上时,我想调用一个函数。基本上我无法使用 QPushButton:hover 因为我要调用的函数非常复杂并且与按钮没有太大关系。这是一个示例代码。
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QWidget
# ----------------------------------------------------
def firstText(myLabel):
myLabel.setText("Wassup yall?")
def secondText(myLabel):
myLabel.setText("Initial Text")
# ----------------------------------------------------
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("Wassup?")
window.setFixedWidth(1000)
window.setFixedHeight(600)
window.move(100, 50)
window.setStyleSheet(
"background-color: rgb(208, 208, 208);"
)
button = QtWidgets.QPushButton(window)
button.setGeometry(100, 50, 120, 60)
button.setStyleSheet(
"QPushButton {"
"border: 0px solid;"
"background-color: rgb(255, 50, 50);"
"}"
)
label = QtWidgets.QLabel(window)
label.setGeometry(100, 120, 120, 60)
label.setStyleSheet(
"background-color: rgb(128, 255, 64);"
)
label.setText("Initial Text")
window.show()
sys.exit(app.exec())
所以我希望当我将鼠标悬停在按钮上时调用函数“firstText”,当我离开悬停时调用函数“secondText”。
【问题讨论】:
标签: python user-interface pyqt5 hover