PyQt5通過使用QSS語法制作精美界面


PyQt做界面很方便,如果需要去美化,可以使用QSS語法達到效果。

效果:

 

實現:

首先設計界面如下,文件awesome.ui,編譯生成Ui_awesome.py:

 

 

新建awesome.py文件,代碼如下:

#coding = utf-8

import sys
from PyQt5.QtWidgets  import QApplication, QMainWindow
from PyQt5.QtCore  import Qt, QSize
from PyQt5.QtGui  import QPalette, QCursor
from Ui_awesome  import Ui_MainWindow
import qtawesome

class  MyWin( QMainWindowUi_MainWindow):
     def  __init__( selfparent =  None):
         super(MyWin,  self). __init__(parent)
         self.setupUi( self)
         self.init_ui()
         self.set_singal()
  
     def  init_ui( self):
         '''
        設置顯示格式
        '''
         self.setWindowTitle( "語音識別")      #設置窗口標題
         #設置窗口圖標
        win_icon = qtawesome.icon( 'fa5s.microphone-alt'color= 'black')
         self.setWindowIcon(win_icon)
         #設置窗口透明度 
         self.setWindowOpacity( 0.9)
         #設置無標題框
         self.setWindowFlag(Qt.FramelessWindowHint)
         #重新設置窗體背景
        pe = QPalette()
         self.setAutoFillBackground( True)
        pe.setColor(QPalette.Window, Qt.lightGray)
         self.setPalette(pe)

         ##通過QSS語法設置控件樣式
         self.btClose.setStyleSheet( '''QPushButton {background:#F76677;border-radius:10px;}
                                      QPushButton:hover {background:red;font-size:14pt} ''')
         self.btMax.setStyleSheet( '''QPushButton {background:#F7D674;border-radius:10px;}
                                    QPushButton:hover {background:yellow;font-size:14pt} ''')
         self.btMin.setStyleSheet( '''QPushButton {background:#6DDF6D;border-radius:10px;}
                                    QPushButton:hover {background:green;font-size:14pt} ''')
         self.lbRecg.setStyleSheet( '''QLabel {color:white;font-size:40px;font-family:Roman times;} ''')
         self.lbRecg.setAlignment(Qt.AlignCenter|Qt.AlignHCenter)

         self.lbDesc.setStyleSheet( '''QLabel{color:darkGray;background:white;border:2px solid #F3F3F5;border-radius:25px;
                font-size:14pt; font-weight:400;font-family: Roman times;} ''')
         self.lbDesc.setAlignment(Qt.AlignCenter|Qt.AlignHCenter)

        spin_icon = qtawesome.icon( 'fa5s.microphone-alt'color= 'white')
         self.btRecord.setIcon(spin_icon) #設置圖標
         self.btRecord.setIconSize(QSize( 50, 50))
         self.btRecord.setStyleSheet( '''QPushButton {border:none;}
                                       QPushButton:hover {color:white; border:2px solid #F3F3F5; border-radius:25px; background:darkGray;} ''')
         pass

     def  set_singal( self):
         '''
        設置信號槽
        '''
         self.btClose.clicked.connect( self.close)
         pass

     def  mousePressEvent( selfevent):
         '''
        設置在鼠標左鍵按下時拖動窗體
        '''
         if event.button() == Qt.LeftButton:
             self.m_flag =  True
             self.m_position = event.globalPos() -  self.pos()     #計算鼠標位置和窗口的相對位置
             print( "global pos: {} , self pos: {} , m pos: {} ".format(event.globalPos(),  self.pos(),  self.m_position))
            event.accept()
             self.setCursor(QCursor(Qt.OpenHandCursor))
         pass

     def  mouseMoveEvent( selfevent):
         if  self.m_flag:
             self.move(event.globalPos() -  self.m_position)   #計算新的窗口位置並移動
            event.accept()

     def  mouseReleaseEvent( selfevent):
         self.m_flag =  False
         self.setCursor(QCursor(Qt.ArrowCursor))


if  __name__ ==  "__main__":
        app = QApplication(sys.argv)
        w = MyWin()
        w.show()
        sys.exit(app.exec_())


對於一個布局內的部分控件進行集體的美化,left_wideget可以看成是一個布局內的控件的集合。

self. my_widget.setStyleSheet( '''
            QPushButton{border:none;color:white;padding-left:5px;
                    height:35px;
                    font-size:15px;
                    padding-right:10px;}
            QPushButton#left_label{
                border:none;
                border-bottom:1px solid white;
                font-size:20px;
                font-weight:700;
                font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
            }
            QWidget#my_widget{
                background:Gray;
                border-top:1px solid white;
                border-bottom:1px solid white;
                border-left:1px solid white;
                border-top-left-radius:10px;
                border-bottom-left-radius:10px;
            }
            QPushButton#left_button:hover{ color:white;
                    border:2px solid #F3F3F5;
                    border-radius:15px;
                    background:black;}
        ''')
 

QPushButton是對my_wideget里面所有的按鈕進行美化。

QPushButton#left_label是對my_wideget中對象的名稱為left_label的按鈕進行美化。

QWidget#my_widget是對對象名為left_widget的QWidget進行美化。

最后面是當對象名為left_button的按鈕懸停上時進行的美化。


其它鏈接:https://blog.csdn.net/liang19890820/article/details/51992137
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM