如何利用PyQt5美化你的GUI界面
python的脚本开发简单,有时候只需几行代码就能实现丰富的功能,而且python本身是跨平台的,所以深受程序员的喜爱,下面这篇文章主要给大家介绍了关于如何利用PyQt5美化你的GUI界面的相关资料,需要的朋友可以参考下
目录
1 圆点选择选项设置
2 选项按钮设置
3 关闭弹窗设置
4 关闭程序弹窗
5 设置关闭按钮
6 设置背景
7 下拉列表框设置
8 等待时显示进度条
总结
1 圆点选择选项设置
效果展示
代码参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #!/usr/bin/python # -*- coding:utf-8 -*- import sys from PyQt5 import QtWidgets, QtCore from PyQt5.QtWidgets import * class qt_view(QWidget): def __init__( self ): super (qt_view, self ).__init__() self .resize( 600 , 250 ) self .setWindowTitle( "圆点选择" ) self .radioButton_1 = QtWidgets.QRadioButton( self ) self .radioButton_1.setGeometry(QtCore.QRect( 230 , 100 , 89 , 16 )) self .radioButton_1.setStyleSheet( "font-family:微软雅黑; color:black;" ) self .radioButton_1.setObjectName( "radioButton_1" ) self .radioButton_2 = QtWidgets.QRadioButton( self ) self .radioButton_2.setGeometry(QtCore.QRect( 310 , 100 , 89 , 16 )) self .radioButton_2.setStyleSheet( "font-family:微软雅黑; color:black;" ) self .radioButton_2.setObjectName( "radioButton_2" ) translate = QtCore.QCoreApplication.translate self .radioButton_1.setText(translate( "Form" , "选项1" )) self .radioButton_2.setText(translate( "Form" , "选项2" )) if __name__ = = '__main__' : app = QtWidgets.QApplication(sys.argv) my = qt_view() my.show() app.exec_() |
2 选项按钮设置
效果展示
代码参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import sys from PyQt5 import QtWidgets, QtCore from PyQt5.QtWidgets import * class qt_view(QWidget): def __init__( self ): super (qt_view, self ).__init__() self .resize( 600 , 250 ) self .setWindowTitle( "圆灰按钮" ) button_open_img = QPushButton( self ) button_open_img.setText( "打开图片" ) button_open_img.move( 250 , 100 ) button_open_img.setFixedSize( 150 , 50 ) button_open_img.setStyleSheet( "QPushButton{\n" " background:orange;\n" " color:white;\n" " box-shadow: 1px 1px 3px;font-size:18px;border-radius: 24px;font-family: 微软雅黑;\n" "}\n" "QPushButton:pressed{\n" " background:black;\n" "}" ) if __name__ = = '__main__' : app = QtWidgets.QApplication(sys.argv) my = qt_view() my.show() app.exec_() |
3 关闭弹窗设置
效果展示
代码参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import sys from PyQt5 import QtWidgets, QtCore from PyQt5.QtWidgets import * class qt_view(QWidget): def __init__( self ): super (qt_view, self ).__init__() print ( "关闭弹窗" ) result = QMessageBox.question( self , "注意!" , "您真的要关闭吗?" , QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if result = = QMessageBox.Yes: QMessageBox.information( self , "消息" , "谢谢使用!" ) quit() else : QMessageBox.information( self , "消息" , "正在返回..." ) quit() if __name__ = = '__main__' : app = QtWidgets.QApplication(sys.argv) my = qt_view() my.show() app.exec_() |
4 关闭程序弹窗
效果展示
代码参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | from PyQt5 import QtWidgets import sys class Ui_Dialog( object ): def setupUi( self , Dialog): Dialog.setObjectName( "Dialog" ) Dialog.resize( 600 , 320 ) class Dialog(QtWidgets.QMainWindow): def closeEvent( self , event): reply = QtWidgets.QMessageBox.question( self , '本程序' , "是否要退出程序?" , QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No) if reply = = QtWidgets.QMessageBox.Yes: event.accept() else : event.ignore() if __name__ = = '__main__' : app = QtWidgets.QApplication(sys.argv) dialog = Dialog() ui = Ui_Dialog() ui.setupUi(dialog) dialog.show() sys.exit(app.exec_()) |
5 设置关闭按钮
效果展示
代码参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | import sys from PyQt5 import QtWidgets, QtCore from PyQt5.QtWidgets import * from PyQt5.QtCore import * class gui_view(QWidget): def __init__( self ): super (gui_view, self ).__init__() self .resize( 500 , 350 ) self .setWindowFlags(Qt.FramelessWindowHint) # 去边框 # # self.setAttribute(Qt.WA_TranslucentBackground) # 设置窗口背景透明 button_red = QPushButton( self ) button_red.move( 20 , 20 ) button_red.setFixedSize( 20 , 20 ) button_red.setStyleSheet( "QPushButton{\n" " background:#CE0000;\n" " color:white;\n" " box-shadow: 1px 1px 3px;border-radius: 10px;\n" "}\n" "QPushButton:hover{ \n" " background:red;\n" "}\n" "QPushButton:pressed{\n" " border: 1px solid #3C3C3C!important;\n" " background:black;\n" "}" ) button_red.clicked.connect( self .quit_button) button_orange = QPushButton( self ) button_orange.move( 50 , 20 ) button_orange.setFixedSize( 20 , 20 ) button_orange.setStyleSheet( "QPushButton{\n" " background:orange;\n" " color:white;\n" " box-shadow: 1px 1px 3px;border-radius: 10px;\n" "}\n" "QPushButton:hover{ \n" " background:yellow;\n" "}\n" "QPushButton:pressed{\n" " border: 1px solid #3C3C3C!important;\n" " background:black;\n" "}" ) button_green = QPushButton( self ) button_green.move( 80 , 20 ) button_green.setFixedSize( 20 , 20 ) button_green.setStyleSheet( "QPushButton{\n" " background:green;\n" " color:white;\n" " box-shadow: 1px 1px 3px;border-radius: 10px;\n" "}\n" "QPushButton:hover{ \n" " background:#08BF14;\n" "}\n" "QPushButton:pressed{\n" " border: 1px solid #3C3C3C!important;\n" " background:black;\n" "}" ) def quit_button( self ): quit() if __name__ = = '__main__' : app2 = QtWidgets.QApplication(sys.argv) my = gui_view() my.show() app2.exec_() |
6 设置背景
效果展示
代码参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import sys from PyQt5 import QtWidgets, QtCore from PyQt5.QtWidgets import * from PyQt5 import QtGui class gui_view(QWidget): def __init__( self ): super (gui_view, self ).__init__() self .resize( 1200 , 750 ) # self.setStyleSheet("background-image: url(:F:/background.jpg);") self .setWindowTitle( "设置背景图片" ) window_pale = QtGui.QPalette() window_pale.setBrush( self .backgroundRole(), QtGui.QBrush(QtGui.QPixmap( "F:/background.jpg" ))) self .setPalette(window_pale) if __name__ = = '__main__' : app2 = QtWidgets.QApplication(sys.argv) my = gui_view() my.show() app2.exec_() |
7 下拉列表框设置
效果展示
代码参考
1 2 3 |