欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何利用PyQt5美化你的GUI界面

程序员文章站 2022-06-17 17:23:49
目录1 圆点选择选项设置2 选项按钮设置3 关闭弹窗设置4 关闭程序弹窗5 设置关闭按钮6 设置背景7 下拉列表框设置8 等待时显示进度条总结1 圆点选择选项设置效果展示代码参考#!/usr/bin/...

1 圆点选择选项设置

效果展示

如何利用PyQt5美化你的GUI界面

代码参考

#!/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 选项按钮设置

效果展示

如何利用PyQt5美化你的GUI界面

代码参考

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 关闭弹窗设置

效果展示

如何利用PyQt5美化你的GUI界面

代码参考

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 关闭程序弹窗

效果展示

如何利用PyQt5美化你的GUI界面

代码参考

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 设置关闭按钮

效果展示

如何利用PyQt5美化你的GUI界面

代码参考

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 设置背景

效果展示

如何利用PyQt5美化你的GUI界面

代码参考

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 下拉列表框设置

效果展示

如何利用PyQt5美化你的GUI界面

代码参考

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 等待时显示进度条

效果展示

如何利用PyQt5美化你的GUI界面

代码参考

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界面的文章就介绍到这了,更多相关pyqt5美化gui界面内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: pyqt gui 界面