问题是,我想做的是在每个按钮上随机显示单词CLICK
。
如果按下带有单词的按钮CLICK
,则它会亮起绿色,但如果按下不带单词的按钮CLICK
,则会以红色突出显示。
请帮忙,到目前为止我还不熟悉PyQt5库。而且我不知道所有的命令。
代码示例:
###
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QVBoxLayout,
QHBoxLayout, QPushButton, QMessageBox
from random import*
###
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Fast Clicker')
self.resize(800,500)
button1 = QPushButton()
button2 = QPushButton()
button3 = QPushButton()
button4 = QPushButton()
h_layout = QHBoxLayout()
h_layout.addWidget(button1)
h_layout.addWidget(button2)
h_layout.addWidget(button3)
h_layout.addWidget(button4)
main_layout = QVBoxLayout(self)
main_layout.addLayout(h_layout)
###
Stylesheet = '''
QWidget {
background-color:rgb(100,200, 255);
}
QPushButton {
background-color:yellow;
max-width: 96px;
max-height: 125px;
border: 8px solid rgb(80, 80, 255);
}
QPushButton:hover:pressed {
background-color: red;
}
'''
###
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
app.setStyleSheet(Stylesheet)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
试试这样: