pyqt5入门到入门 [配合pycharm]

https://www.lagou.com/lgeduarticle/109104.html

-m PyQt5.uic.pyuic FileNameFileName -o FileNameWithoutExtensionFileNameWithoutExtension.py

1. 安装库 [Python version>3.5]

pip install PyQt5
pip install PyQt5-tools

2. 配置pycharm额外插件

image-20200607003545564

image-20200607003630807

Name:Qt Designer
Programs:D:\Program Files\Python35\Lib\site-packages\pyqt5-tools\designer.exe
Working directory:$ProjectFileDir$

img

Name:PyUIC
Programs:D:\Program Files\Python35\python.exe
Parameters:-m PyQt5.uic.pyuic  $FileName$ -o $FileNameWithoutExtension$.py
Working directory:$ProjectFileDir$

img

3. 设计界面

组件设置布局、布局添加组件

3.0 Qt元素分类

QWidget

3.0.1 容器
  • 布局Layout [QH\QV\QGrid]
  • 分组容器 [QGroupBox\QTabWidget]
3.0.2 组件

QLabel,QCheckBox, QRadioButton,QComboBox,QLineEdit, QTextEdit, QDialog, QPushButton,QSlider, QScrollBar, QDateTimeEdit,QSpinBox, QTableWidget, QProgressBar, QDial,QStyleFactory,QSizePolicy

3.1 在上面弄个水平布局,并添加两个组件

from PyQt5.QtCore import QDateTime, QTimer, Qt
from PyQt5.QtWidgets import (QApplication, QGridLayout, QHBoxLayout,
                             QVBoxLayout, QGroupBox, QTabWidget, QLabel,
                             QCheckBox, QRadioButton,
                             QComboBox,
                             QLineEdit, QTextEdit, QDialog, QPushButton,
                             QSlider, QScrollBar,
                             QSpinBox, QTableWidget, QProgressBar, QDial,
                             QWidget, QStyleFactory,
                             QSizePolicy)


class WidgetGallery(QDialog):
    def __init__(self, parent=None):
        # 设置自定义的样式
        super(WidgetGallery, self).__init__(parent)
        self.originalPalette = QApplication.palette()

        # 下拉列表
        style_combobox = QComboBox()
        style_combobox.addItems(QStyleFactory.keys())

        # 标签
        style_label = QLabel('样式')
        style_label.setBuddy(style_combobox)

        self.use_stander_checkbox = QCheckBox('使用标准的样式')
        self.use_stander_checkbox.setChecked(True)

        disable_stander_checkbox = QCheckBox('禁用组件')

        # 顶端布局
        top_layout = QHBoxLayout()  # 水平布局
        top_layout.addWidget(style_label)
        top_layout.addWidget(style_combobox)
        top_layout.addWidget(self.use_stander_checkbox)
        top_layout.addWidget(disable_stander_checkbox)

        self.setLayout(top_layout)  # 将整个对象布局设置为这个布局
        self.setWindowTitle('基本组件')




if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    gallery = WidgetGallery()
    gallery.show()
    sys.exit(app.exec_())

image-20200607134415995

3.2 需要更改一下全局布局 改为main_layout,并在布局里面加东西

from PyQt5.QtCore import QDateTime, QTimer, Qt
from PyQt5.QtWidgets import (QApplication, QGridLayout, QHBoxLayout,
                             QVBoxLayout, QGroupBox, QTabWidget, QLabel,
                             QCheckBox, QRadioButton,
                             QComboBox,
                             QLineEdit, QTextEdit, QDialog, QPushButton,
                             QSlider, QScrollBar,
                             QSpinBox, QTableWidget, QProgressBar, QDial,
                             QWidget, QStyleFactory,
                             QSizePolicy)


class WidgetGallery(QDialog):
    def __init__(self, parent=None):
        # 设置自定义的样式
        super(WidgetGallery, self).__init__(parent)
        self.originalPalette = QApplication.palette()

        # 下拉列表
        style_combobox = QComboBox()
        style_combobox.addItems(QStyleFactory.keys())

        # 标签
        style_label = QLabel('样式')
        style_label.setBuddy(style_combobox)

        self.use_stander_checkbox = QCheckBox('使用标准的样式')
        self.use_stander_checkbox.setChecked(True)

        disable_stander_checkbox = QCheckBox('禁用组件')
        self.createTopLeftGroupBox()
        # 顶端布局
        top_layout = QHBoxLayout()  # 水平布局
        top_layout.addWidget(style_label)
        top_layout.addWidget(style_combobox)
        top_layout.addWidget(self.use_stander_checkbox)
        top_layout.addWidget(disable_stander_checkbox)
        main_layout = QGridLayout()
        main_layout.addLayout(top_layout, 0, 0, 1, 2)
        # 参数:第一行,第一列,放在一行不合并,将两列合并
        main_layout.addWidget(self.top_left_group, 1, 0)
        self.setLayout(main_layout)  # 将整个对象布局设置为这个布局
        self.setWindowTitle('基本组件')

    # 左上角:第一组基本组件
    def createTopLeftGroupBox(self):
        self.top_left_group = QGroupBox('第一组')
        radiobutton1 = QRadioButton('单选一')
        radiobutton2 = QRadioButton('单选二')
        radiobutton3 = QRadioButton('单选三')

        checkbox = QCheckBox('Tri-state checkbox')
        checkbox.setTristate(True)
        checkbox.setCheckState(Qt.PartiallyChecked)

        # 垂直布局
        layout = QVBoxLayout()
        layout.addWidget(radiobutton1)
        layout.addWidget(radiobutton2)
        layout.addWidget(radiobutton3)
        layout.addWidget(checkbox)
        self.top_left_group.setLayout(layout)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    gallery = WidgetGallery()
    gallery.show()
    sys.exit(app.exec_())

image-20200607135310163

3.3 需要修改一下全局布局

    def createTopRightGroupBox(self):
        self.top_right_group = QGroupBox('第二组')

        defatlt_button = QPushButton('默认的样式Button')
        defatlt_button.setDefault(True)
        toggle_button = QPushButton('开关的Button')
        toggle_button.setCheckable(True)
        toggle_button.setChecked(True)

        flat_button = QPushButton('FlatButton')
        flat_button.setFlat(True)

        layout = QVBoxLayout()
        layout.addWidget(defatlt_button)
        layout.addWidget(toggle_button)
        layout.addWidget(flat_button)
        self.top_right_group.setLayout(layout)

image-20200607140650932

3.4 表格\标签组件

    def createBottomLeftTabWidget(self):
        # 选项卡
        self.bottom_left_tabwidget = QTabWidget()  # 有选项卡 就有标签页
        self.bottom_left_tabwidget.setSizePolicy(
            QSizePolicy.Preferred, QSizePolicy.Ignored)
        # 标签
        tab1 = QWidget()
        tablewidget = QTableWidget(10, 10)
        tab1hlayout = QHBoxLayout()
        tab1hlayout.addWidget(tablewidget)
        tab1.setLayout(tab1hlayout)

        tab2 = QWidget()
        textedit = QTextEdit()
        textedit.setPlainText('123456\n'
                              '456789\n')
        tab2hlayout = QHBoxLayout()
        tab2hlayout.addWidget(textedit)
        tab2.setLayout(tab2hlayout)
        self.bottom_left_tabwidget.addTab(tab1, '表格')
        self.bottom_left_tabwidget.addTab(tab2, '文本')

image-20200607142614359

3.5 滑动条、滑块、dial

def createBottomRightGroupBox(self):
        self.bottom_right_group = QGroupBox()
        lineedit = QLineEdit('str3')
        lineedit.setEchoMode(QLineEdit.Password)
        spinbox = QSpinBox(self.bottom_right_group)
        spinbox.setValue(20)

        datetimeedit = QDateTimeEdit(self.bottom_right_group)  # 时间
        datetimeedit.setDateTime(QDateTime.currentDateTime())  # 获取当前时间

        slider = QSlider(Qt.Horizontal, self.bottom_right_group)
        slider.setValue(50)

        scrollbar = QScrollBar(Qt.Horizontal, self.bottom_right_group)
        scrollbar.setValue(40)

        dial = QDial(self.bottom_right_group)
        dial.setValue(30)
        dial.setNotchesVisible(True)

        layout = QGridLayout()
        layout.addWidget(lineedit, 0, 0, 1, 2)
        layout.addWidget(spinbox, 1, 0, 1, 2)
        layout.addWidget(datetimeedit, 2, 0, 1, 2)
        layout.addWidget(slider, 3, 0)
        layout.addWidget(scrollbar, 4, 0)
        layout.addWidget(dial, 3, 1, 2, 1)
        self.bottom_right_group.setLayout(layout)

image-20200607145600956

3.6 进度条

    # 构建进度条
    def createProgressbar(self):
        self.progressbar = QProgressBar()
        self.progressbar.setRange(0, 1000)
        self.progressbar.setValue(0)

        timer = QTimer(self)
        # qt事件处理机制
        timer.timeout.connect(self.advanceProgressbar)
        timer.start(1000)  # 毫秒

    def advanceProgressbar(self):
        cur_val = self.progressbar.value()
        max_val = self.progressbar.maximum()
        self.progressbar.setValue(cur_val+(max_val-cur_val)/100)

image-20200607145735432

3.7 改变样式模板

## 在init里面更改
# 使用下拉列表更改
style_combobox.activated[str].connect(self.changeStyle)
self.use_stander_checkbox.toggled.connect(self.changePalette)
    def changePalette(self):
        if self.use_stander_checkbox.isChecked():
            QApplication.setPalette(QApplication.style().standardPalette())
        else:
            QApplication.setPalette(self.originalPalette)

    def changeStyle(self,style_name):
        QApplication.setStyle(QStyleFactory.create(style_name))
        self.changePalette()
def __init__(self, parent=None):
        # 设置自定义的样式
        super(WidgetGallery, self).__init__(parent)
        self.originalPalette = QApplication.palette()

        # 下拉列表
        style_combobox = QComboBox()
        style_combobox.addItems(QStyleFactory.keys())

        # 标签
        style_label = QLabel('样式')
        style_label.setBuddy(style_combobox)

        self.use_stander_checkbox = QCheckBox('使用标准的样式')
        self.use_stander_checkbox.setChecked(True)

        # 使用下拉列表更改
        style_combobox.activated[str].connect(self.changeStyle)
        self.use_stander_checkbox.toggled.connect(self.changePalette)

        disable_stander_checkbox = QCheckBox('禁用组件')
        self.createTopLeftGroupBox()
        self.createTopRightGroupBox()
        self.createBottomLeftTabWidget()
        self.createBottomRightGroupBox()
        self.createProgressbar()
        # 顶端布局
        top_layout = QHBoxLayout()  # 水平布局
        top_layout.addWidget(style_label)
        top_layout.addWidget(style_combobox)
        top_layout.addWidget(self.use_stander_checkbox)
        top_layout.addWidget(disable_stander_checkbox)
        main_layout = QGridLayout()
        main_layout.addLayout(top_layout, 0, 0, 1, 2)
        # 参数:第一行,第一列,放在一行不合并,将两列合并
        main_layout.addWidget(self.top_left_group, 1, 0)  # 网格
        main_layout.addWidget(self.top_right_group, 1, 1)
        main_layout.addWidget(self.bottom_left_tabwidget, 2, 0)
        main_layout.addWidget(self.bottom_right_group, 2, 1)
        main_layout.addWidget(self.progressbar,3,0,1,2)
        self.setLayout(main_layout)  # 将整个对象布局设置为这个布局
        self.setWindowTitle('基本组件')

image-20200607151205895

3.8 全部代码

from PyQt5.QtCore import QDateTime, QTimer, Qt
from PyQt5.QtWidgets import (QApplication, QGridLayout, QHBoxLayout,
                             QVBoxLayout, QGroupBox, QTabWidget, QLabel,
                             QCheckBox, QRadioButton,
                             QComboBox,
                             QLineEdit, QTextEdit, QDialog, QPushButton,
                             QSlider, QScrollBar, QDateTimeEdit,
                             QSpinBox, QTableWidget, QProgressBar, QDial,
                             QWidget, QStyleFactory,
                             QSizePolicy)


class WidgetGallery(QDialog):
    def __init__(self, parent=None):
        # 设置自定义的样式
        super(WidgetGallery, self).__init__(parent)
        self.originalPalette = QApplication.palette()

        # 下拉列表
        style_combobox = QComboBox()
        style_combobox.addItems(QStyleFactory.keys())

        # 标签
        style_label = QLabel('样式')
        style_label.setBuddy(style_combobox)

        self.use_stander_checkbox = QCheckBox('使用标准的样式')
        self.use_stander_checkbox.setChecked(True)

        # 使用下拉列表更改
        style_combobox.activated[str].connect(self.changeStyle)
        self.use_stander_checkbox.toggled.connect(self.changePalette)

        disable_stander_checkbox = QCheckBox('禁用组件')
        self.createTopLeftGroupBox()
        self.createTopRightGroupBox()
        self.createBottomLeftTabWidget()
        self.createBottomRightGroupBox()
        self.createProgressbar()
        # 顶端布局
        top_layout = QHBoxLayout()  # 水平布局
        top_layout.addWidget(style_label)
        top_layout.addWidget(style_combobox)
        top_layout.addWidget(self.use_stander_checkbox)
        top_layout.addWidget(disable_stander_checkbox)
        main_layout = QGridLayout()
        main_layout.addLayout(top_layout, 0, 0, 1, 2)
        # 参数:第一行,第一列,放在一行不合并,将两列合并
        main_layout.addWidget(self.top_left_group, 1, 0)  # 网格
        main_layout.addWidget(self.top_right_group, 1, 1)
        main_layout.addWidget(self.bottom_left_tabwidget, 2, 0)
        main_layout.addWidget(self.bottom_right_group, 2, 1)
        main_layout.addWidget(self.progressbar,3,0,1,2)
        self.setLayout(main_layout)  # 将整个对象布局设置为这个布局
        self.setWindowTitle('基本组件')

    # 左上角:第一组基本组件
    def createTopLeftGroupBox(self):
        self.top_left_group = QGroupBox('第一组')
        radiobutton1 = QRadioButton('单选一')
        radiobutton2 = QRadioButton('单选二')
        radiobutton3 = QRadioButton('单选三')

        checkbox = QCheckBox('Tri-state checkbox')
        checkbox.setTristate(True)
        checkbox.setCheckState(Qt.PartiallyChecked)

        # 垂直布局
        layout = QVBoxLayout()
        layout.addWidget(radiobutton1)
        layout.addWidget(radiobutton2)
        layout.addWidget(radiobutton3)
        layout.addWidget(checkbox)
        self.top_left_group.setLayout(layout)

    def createTopRightGroupBox(self):
        self.top_right_group = QGroupBox('第二组')

        defatlt_button = QPushButton('默认的样式Button')
        defatlt_button.setDefault(True)
        toggle_button = QPushButton('开关的Button')
        toggle_button.setCheckable(True)
        toggle_button.setChecked(True)

        flat_button = QPushButton('FlatButton')
        flat_button.setFlat(True)

        layout = QVBoxLayout()
        layout.addWidget(defatlt_button)
        layout.addWidget(toggle_button)
        layout.addWidget(flat_button)
        self.top_right_group.setLayout(layout)

    # 左下角 构建选项卡
    def createBottomLeftTabWidget(self):
        # 选项卡
        self.bottom_left_tabwidget = QTabWidget()  # 有选项卡 就有标签页
        self.bottom_left_tabwidget.setSizePolicy(
            QSizePolicy.Preferred, QSizePolicy.Ignored)
        # 标签
        tab1 = QWidget()
        tablewidget = QTableWidget(10, 10)
        tab1hlayout = QHBoxLayout()
        tab1hlayout.addWidget(tablewidget)
        tab1.setLayout(tab1hlayout)

        tab2 = QWidget()
        textedit = QTextEdit()
        textedit.setPlainText('123456\n'
                              '456789\n')
        tab2hlayout = QHBoxLayout()
        tab2hlayout.addWidget(textedit)
        tab2.setLayout(tab2hlayout)
        self.bottom_left_tabwidget.addTab(tab1, '表格')
        self.bottom_left_tabwidget.addTab(tab2, '文本')

    # 右下角,第三组
    def createBottomRightGroupBox(self):
        self.bottom_right_group = QGroupBox()
        lineedit = QLineEdit('str3')
        lineedit.setEchoMode(QLineEdit.Password)
        spinbox = QSpinBox(self.bottom_right_group)
        spinbox.setValue(20)

        datetimeedit = QDateTimeEdit(self.bottom_right_group)  # 时间
        datetimeedit.setDateTime(QDateTime.currentDateTime())  # 获取当前时间

        slider = QSlider(Qt.Horizontal, self.bottom_right_group)
        slider.setValue(50)

        scrollbar = QScrollBar(Qt.Horizontal, self.bottom_right_group)
        scrollbar.setValue(40)

        dial = QDial(self.bottom_right_group)
        dial.setValue(30)
        dial.setNotchesVisible(True)

        layout = QGridLayout()
        layout.addWidget(lineedit, 0, 0, 1, 2)
        layout.addWidget(spinbox, 1, 0, 1, 2)
        layout.addWidget(datetimeedit, 2, 0, 1, 2)
        layout.addWidget(slider, 3, 0)
        layout.addWidget(scrollbar, 4, 0)
        layout.addWidget(dial, 3, 1, 2, 1)
        self.bottom_right_group.setLayout(layout)

    # 构建进度条
    def createProgressbar(self):
        self.progressbar = QProgressBar()
        self.progressbar.setRange(0, 1000)
        self.progressbar.setValue(0)

        timer = QTimer(self)
        # qt事件处理机制
        timer.timeout.connect(self.advanceProgressbar)
        timer.start(1000)  # 毫秒

    def advanceProgressbar(self):
        cur_val = self.progressbar.value()
        max_val = self.progressbar.maximum()
        self.progressbar.setValue(cur_val+(max_val-cur_val)/100)

    def changePalette(self):
        if self.use_stander_checkbox.isChecked():
            QApplication.setPalette(QApplication.style().standardPalette())
        else:
            QApplication.setPalette(self.originalPalette)

    def changeStyle(self,style_name):
        QApplication.setStyle(QStyleFactory.create(style_name))
        self.changePalette()
if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    gallery = WidgetGallery()
    gallery.show()
    sys.exit(app.exec_())

4. QtDesigner

选择容器元素然后。。添加组件 [修改变量名,方便区分,编码规范]

image-20200607164000084

image-20200607163925682

from PyQt5.QtWidgets import *
from cut_ui.mytest import Ui_Dialog  # 从包的路径开始找

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    main = QDialog()
    login_dialog = Ui_Dialog()
    login_dialog.setupUi(main)
    main.show()
    sys.exit(app.exec_())  # 一直监听事件

©️2020 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值