如何利用PyQt5美化你的GUI界面
作者:荣仔!最靓的仔! 时间:2023-06-18 19:58:55
1 圆点选择选项设置
效果展示
代码参考
#!/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 选项按钮设置
效果展示
代码参考
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 关闭弹窗设置
效果展示
代码参考
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 关闭程序弹窗
效果展示
代码参考
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 设置关闭按钮
效果展示
代码参考
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 设置背景
效果展示
代码参考
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 下拉列表框设置
效果展示
代码参考
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 等待时显示进度条
效果展示
代码参考
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_())
来源:https://blog.csdn.net/IT_charge/article/details/120003164
标签:pyqt,gui,界面
0
投稿
猜你喜欢
基于 Dubbo Admin 动态调整服务超时时间的操作步骤
2023-01-06 13:53:54
python 实现人和电脑猜拳的示例代码
2021-04-11 01:42:14
深入解析Python中的list列表及其切片和迭代操作
2023-03-24 04:20:40
空间session失效过快的解决办法
2010-09-15 10:01:00
Python实现简易信息分类存储软件
2023-08-09 20:45:08
Python 工具类实现大文件断点续传功能详解
2022-11-17 05:41:29
企业级使用LAMP源码安装教程
2024-01-17 19:41:29
vue+element upload上传带参数的实例
2024-05-09 09:32:56
js运算精度丢失的2个解决方法
2024-04-10 10:38:02
Python opencv应用实现图片切分操作示例
2021-12-25 03:45:39
MySQL常用SQL语句总结包含复杂SQL查询
2024-01-14 09:37:26
python GUI库图形界面开发之PyQt5滚动条控件QScrollBar详细使用方法与实例
2021-04-26 09:11:26
Vue全局事件总线你了解吗
2024-04-28 09:25:11
如何使用python爬取知乎热榜Top50数据
2021-11-13 05:47:09
Bootstrap modal使用及点击外部不消失的解决方法
2024-04-10 13:56:45
Python datatime库语法使用详解
2022-01-30 03:16:17
详解Django模版中加载静态文件配置方法
2023-11-16 19:55:13
Python(PyS60)实现简单语音整点报时
2023-07-06 03:05:23
python模拟哔哩哔哩滑块登入验证的实现
2021-05-01 22:23:40
python Yaml、Json、Dict之间的转化
2022-05-15 02:26:17