当前位置:   article > 正文

PyQt5美化你的GUI界面_pyqt5开发的漂亮界面

pyqt5开发的漂亮界面

目录

1 圆点选择选项设置

2 选项按钮设置

3 关闭弹窗设置

4 关闭程序弹窗

5 设置关闭按钮

6 设置背景

7 下拉列表框设置

8 等待时显示进度条


1 圆点选择选项设置

效果展示

代码参考

  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. import sys
  4. from PyQt5 import QtWidgets, QtCore
  5. from PyQt5.QtWidgets import *
  6. class qt_view(QWidget):
  7. def __init__(self):
  8. super(qt_view, self).__init__()
  9. self.resize(600, 250)
  10. self.setWindowTitle("圆点选择")
  11. self.radioButton_1 = QtWidgets.QRadioButton(self)
  12. self.radioButton_1.setGeometry(QtCore.QRect(230, 100, 89, 16))
  13. self.radioButton_1.setStyleSheet("font-family:微软雅黑; color:black;")
  14. self.radioButton_1.setObjectName("radioButton_1")
  15. self.radioButton_2 = QtWidgets.QRadioButton(self)
  16. self.radioButton_2.setGeometry(QtCore.QRect(310, 100, 89, 16))
  17. self.radioButton_2.setStyleSheet("font-family:微软雅黑; color:black;")
  18. self.radioButton_2.setObjectName("radioButton_2")
  19. translate = QtCore.QCoreApplication.translate
  20. self.radioButton_1.setText(translate("Form", "选项1"))
  21. self.radioButton_2.setText(translate("Form", "选项2"))
  22. if __name__ == '__main__':
  23. app = QtWidgets.QApplication(sys.argv)
  24. my = qt_view()
  25. my.show()
  26. app.exec_()

2 选项按钮设置

效果展示

代码参考

  1. import sys
  2. from PyQt5 import QtWidgets, QtCore
  3. from PyQt5.QtWidgets import *
  4. class qt_view(QWidget):
  5. def __init__(self):
  6. super(qt_view, self).__init__()
  7. self.resize(600, 250)
  8. self.setWindowTitle("圆灰按钮")
  9. button_open_img = QPushButton(self)
  10. button_open_img.setText("打开图片")
  11. button_open_img.move(250, 100)
  12. button_open_img.setFixedSize(150, 50)
  13. button_open_img.setStyleSheet("QPushButton{\n"
  14. " background:orange;\n"
  15. " color:white;\n"
  16. " box-shadow: 1px 1px 3px;font-size:18px;border-radius: 24px;font-family: 微软雅黑;\n"
  17. "}\n"
  18. "QPushButton:pressed{\n"
  19. " background:black;\n"
  20. "}")
  21. if __name__ == '__main__':
  22. app = QtWidgets.QApplication(sys.argv)
  23. my = qt_view()
  24. my.show()
  25. app.exec_()

3 关闭弹窗设置

效果展示

代码参考

  1. import sys
  2. from PyQt5 import QtWidgets, QtCore
  3. from PyQt5.QtWidgets import *
  4. class qt_view(QWidget):
  5. def __init__(self):
  6. super(qt_view, self).__init__()
  7. print("关闭弹窗")
  8. result = QMessageBox.question(self, "注意!", "您真的要关闭吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
  9. if result == QMessageBox.Yes:
  10. QMessageBox.information(self, "消息", "谢谢使用!")
  11. quit()
  12. else:
  13. QMessageBox.information(self, "消息", "正在返回...")
  14. quit()
  15. if __name__ == '__main__':
  16. app = QtWidgets.QApplication(sys.argv)
  17. my = qt_view()
  18. my.show()
  19. app.exec_()

4 关闭程序弹窗

效果展示

代码参考

  1. from PyQt5 import QtWidgets
  2. import sys
  3. class Ui_Dialog(object):
  4. def setupUi(self, Dialog):
  5. Dialog.setObjectName("Dialog")
  6. Dialog.resize(600, 320)
  7. class Dialog(QtWidgets.QMainWindow):
  8. def closeEvent(self, event):
  9. reply = QtWidgets.QMessageBox.question(self,
  10. '本程序',
  11. "是否要退出程序?",
  12. QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
  13. QtWidgets.QMessageBox.No)
  14. if reply == QtWidgets.QMessageBox.Yes:
  15. event.accept()
  16. else:
  17. event.ignore()
  18. if __name__ == '__main__':
  19. app = QtWidgets.QApplication(sys.argv)
  20. dialog = Dialog()
  21. ui = Ui_Dialog()
  22. ui.setupUi(dialog)
  23. dialog.show()
  24. sys.exit(app.exec_())

5 设置关闭按钮

效果展示

代码参考

  1. import sys
  2. from PyQt5 import QtWidgets, QtCore
  3. from PyQt5.QtWidgets import *
  4. from PyQt5.QtCore import *
  5. class gui_view(QWidget):
  6. def __init__(self):
  7. super(gui_view, self).__init__()
  8. self.resize(500, 350)
  9. self.setWindowFlags(Qt.FramelessWindowHint) # 去边框
  10. # # self.setAttribute(Qt.WA_TranslucentBackground) # 设置窗口背景透明
  11. button_red = QPushButton(self)
  12. button_red.move(20, 20)
  13. button_red.setFixedSize(20, 20)
  14. button_red.setStyleSheet("QPushButton{\n"
  15. " background:#CE0000;\n"
  16. " color:white;\n"
  17. " box-shadow: 1px 1px 3px;border-radius: 10px;\n"
  18. "}\n"
  19. "QPushButton:hover{ \n"
  20. " background:red;\n"
  21. "}\n"
  22. "QPushButton:pressed{\n"
  23. " border: 1px solid #3C3C3C!important;\n"
  24. " background:black;\n"
  25. "}")
  26. button_red.clicked.connect(self.quit_button)
  27. button_orange = QPushButton(self)
  28. button_orange.move(50, 20)
  29. button_orange.setFixedSize(20, 20)
  30. button_orange.setStyleSheet("QPushButton{\n"
  31. " background:orange;\n"
  32. " color:white;\n"
  33. " box-shadow: 1px 1px 3px;border-radius: 10px;\n"
  34. "}\n"
  35. "QPushButton:hover{ \n"
  36. " background:yellow;\n"
  37. "}\n"
  38. "QPushButton:pressed{\n"
  39. " border: 1px solid #3C3C3C!important;\n"
  40. " background:black;\n"
  41. "}")
  42. button_green = QPushButton(self)
  43. button_green.move(80, 20)
  44. button_green.setFixedSize(20, 20)
  45. button_green.setStyleSheet("QPushButton{\n"
  46. " background:green;\n"
  47. " color:white;\n"
  48. " box-shadow: 1px 1px 3px;border-radius: 10px;\n"
  49. "}\n"
  50. "QPushButton:hover{ \n"
  51. " background:#08BF14;\n"
  52. "}\n"
  53. "QPushButton:pressed{\n"
  54. " border: 1px solid #3C3C3C!important;\n"
  55. " background:black;\n"
  56. "}")
  57. def quit_button(self):
  58. quit()
  59. if __name__ == '__main__':
  60. app2 = QtWidgets.QApplication(sys.argv)
  61. my = gui_view()
  62. my.show()
  63. app2.exec_()

6 设置背景

效果展示

代码参考

  1. import sys
  2. from PyQt5 import QtWidgets, QtCore
  3. from PyQt5.QtWidgets import *
  4. from PyQt5 import QtGui
  5. class gui_view(QWidget):
  6. def __init__(self):
  7. super(gui_view, self).__init__()
  8. self.resize(1200, 750)
  9. # self.setStyleSheet("background-image: url(:F:/background.jpg);")
  10. self.setWindowTitle("设置背景图片")
  11. window_pale = QtGui.QPalette()
  12. window_pale.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap("F:/background.jpg")))
  13. self.setPalette(window_pale)
  14. if __name__ == '__main__':
  15. app2 = QtWidgets.QApplication(sys.argv)
  16. my = gui_view()
  17. my.show()
  18. app2.exec_()

7 下拉列表框设置

效果展示

代码参考

  1. import sys
  2. from PyQt5.QtWidgets import QWidget, QComboBox, QApplication
  3. class ComboxDemo(QWidget):
  4. def __init__(self):
  5. super().__init__()
  6. self.setWindowTitle('下拉列表框')
  7. self.resize(700, 400)
  8. # 实例化QComBox对象
  9. self.cb = QComboBox(self)
  10. self.cb.move(100, 20)
  11. # 单个添加条目
  12. self.cb.addItem('选项1')
  13. self.cb.addItem('选项2')
  14. # 多个添加条目
  15. self.cb.addItems(['选项3', '选项4', '选项5'])
  16. self.cb.currentIndexChanged[str].connect(self.print_value)
  17. def print_value(self, value):
  18. print(value)
  19. if __name__ == '__main__':
  20. app = QApplication(sys.argv)
  21. comboxDemo = ComboxDemo()
  22. comboxDemo.show()
  23. sys.exit(app.exec_())

8 等待时显示进度条

效果展示

代码参考

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

持续更新中...

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/985368
推荐阅读
相关标签
  

闽ICP备14008679号