pyqt5設置按鈕顏色

找到相應的按鈕,如 pushButton

self.pushButton.setStyleSheet('''QPushButton{background:#F7D674;border-radius:5px;}QPushButton:hover{background:yellow;}''')

當鼠標不在按鈕上時,按鈕顏色爲:#F7D674,當鼠標指向按鈕時,其顏色爲:yellow

相應地,我們可以設置第二個按鈕的顏色:

self.pushButton_2.setStyleSheet('''QPushButton{background:#6DDF6D;border-radius:5px;}QPushButton:hover{background:green;}''')

進階:

button1.setStyleSheet("
QPushButton{font-family:'宋體';font-size:30px;color:rgb(0,0,0,255);}\
QPushButton{background-color:rgb(170,200,50)}\ QPushButton:hover{background-color:rgb(50, 170, 200)}")

將按鈕的字體設置爲宋體,字號爲30,顏色使用的是三原色,color:rgb(0,0,0,255); 前三個參數由0-255之間組合字的顏色,最後一個參數是字的透明度,255是指完全不透明,0是指全透明。

QPushButton{background-color:rgb(170,200,50)} 是將QPushButton 按鈕的背景色設置顏色。

QPushButton:hover{background-color:rgb(50, 170, 200)}") 是當鼠標移動到 QPushButton 按鈕時會變成一種顏色。


設置按鈕的最大尺寸和最小尺寸

button1.setMaximumSize(a, b) 
button1.setMinimumSize(a , b)
QPushButton 按鈕設置大小顏色以及字體的大小

字體設置參考

    self.pushButton.setStyleSheet('''QPushButton{background:orange;border-radius:5px;}QPushButton:hover{background:#9AFF9A;}''')
    self.pushButton_2.setStyleSheet('''QPushButton{background:gold;border-radius:5px;}QPushButton:hover{background:#9AFF9A;}''')
    self.pushButton_3.setStyleSheet('''QPushButton{background:#EEAEEE;border-radius:5px;}QPushButton:hover{background:#9AFF9A;}''')
    self.pushButton_4.setStyleSheet('''QPushButton{background:#fdb933;border-radius:5px;}QPushButton:hover{background:#9AFF9A;}''')
    self.pushButton_5.setStyleSheet('''QPushButton{background:#afb4db;border-radius:5px;}QPushButton:hover{background:#9AFF9A;}''')
    self.pushButton_6.setStyleSheet('''QPushButton{background:#f7acbc;border-radius:5px;}QPushButton:hover{background:#9AFF9A;}''')
    self.pushButton_7.setStyleSheet('''QPushButton{background:#cbc547;border-radius:5px;}QPushButton:hover{background:#9AFF9A;}''')
    self.pushButton_8.setStyleSheet('''QPushButton{background:#90d7ec;border-radius:5px;}QPushButton:hover{background:#9AFF9A}''')

RGB顏色查詢對照表

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章