阅读 174

如何利用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
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
import sys
from PyQt5.QtWidgets import QWidget, QComboBox, QApplication
  
class ComboxDemo(QWidget):
    def __init__(self):
        super().__init__()
  
        self.setWindowTitle('下拉列表框')
        self.resize(700, 400)
  
        # 实例化QComBox对象
        self.cb = QComboBox(self)
        self.cb.move(100, 20)
  
        # 单个添加条目
        self.cb.addItem('选项1')
        self.cb.addItem('选项2')
        # 多个添加条目
        self.cb.addItems(['选项3', '选项4', '选项5'])
  
        self.cb.currentIndexChanged[str].connect(self.print_value) 
  
    def print_value(self, value):
        print(value)
  
if __name__ == '__main__':
    app = QApplication(sys.argv)
    comboxDemo = ComboxDemo()
    comboxDemo.show()
    sys.exit(app.exec_())

8 等待时显示进度条

效果展示

代码参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from PyQt5.QtWidgets import QMainWindow, QProgressBar, QApplication, QLabel, QStatusBar, QPushButton
import sys
  
class SampleBar(QMainWindow):
    def __init__(self, parent=None):
        super(SampleBar, self).__init__(parent)
        self.setMinimumSize(400, 100)
        self.statusBar = QStatusBar()
        self.statusBar.setStyleSheet('QStatusBar::item {border: none;}')
        self.setStatusBar(self.statusBar)
        self.progressBar = QProgressBar()
        self.label = QLabel()
        self.label.setText("加载中,请稍后... ")
        self.statusBar.addPermanentWidget(self.label, stretch=2)
        self.statusBar.addPermanentWidget(self.progressBar, stretch=4)
        self.progressBar.setRange(0, 100)
        self.progressBar.setMinimum(0)
        self.progressBar.setMaximum(0)
  
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = SampleBar()
    main.show()
    sys.exit(app.exec_())

总结

到此这篇关于如何利用PyQt5美化你的GUI界面的文章就介绍到这了

原文链接:https://blog.csdn.net/IT_charge/article/details/120003164


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐