Home > Backend Development > Python Tutorial > Checkboxes with labels that you must learn every day in PyQt5

Checkboxes with labels that you must learn every day in PyQt5

不言
Release: 2018-04-19 13:43:40
Original
2816 people have browsed it

This article mainly introduces in detail the relevant information about check boxes that must be learned every day in PyQt5. It has certain reference value. Interested friends can refer to it

QCheckBox has two types Controls for states: on and off. It's a checkbox with a label. Checkboxes are often used to represent features that an application can enable or disable.


#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
PyQt5 教程

在这个例子中,一个QCheckBox控件用于切换窗口的标题。

作者:我的世界你曾经来过
博客:http://blog.csdn.net/weiaitaowang
最后编辑:2016年8月3日
"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QCheckBox
from PyQt5.QtCore import Qt

class Example(QWidget):

  def __init__(self):
    super().__init__()

    self.initUI()

  def initUI(self):

    cb = QCheckBox('显示标题', self)
    cb.move(20, 20)
    cb.toggle()
    cb.stateChanged.connect(self.changeTitle)

    self.setGeometry(300, 300, 250, 150)
    self.setWindowTitle('复选框')    
    self.show()

  def changeTitle(self, state):

    if state == Qt.Checked:
      self.setWindowTitle('复选框')
    else:
      self.setWindowTitle('')

if __name__ == '__main__':

  app = QApplication(sys.argv)
  ex = Example()
  sys.exit(app.exec_())
Copy after login


In our example, we will create a checkbox to toggle the window title.


cb = QCheckBox('显示标题', self)
Copy after login


Use QCheckBox to create a check box


cb.toggle()
Copy after login


The title of the control has been set when creating the check box. By default, the checkbox is unchecked. toggle is to set the checkbox to be selected by default.


cb.stateChanged.connect(self.changeTitle)
Copy after login


The checkbox's state changes stateChanged signal is connected to the changeTitle method (slot).


def changeTitle(self, state):

    if state == Qt.Checked:
      self.setWindowTitle('复选框')
    else:
      self.setWindowTitle('')
Copy after login


The state of the check box control changes to the parameter state passed to the changeTitle method. Sets the window's title if the checkbox control is selected. Otherwise, we set the title bar to an empty string. (Python will define the title as Python when the title is empty)

After the program is executed

Checkboxes with labels that you must learn every day in PyQt5Checkboxes with labels that you must learn every day in PyQt5

Related recommendations:

PyQt5 must learn every day to create a window centering effect

PyQt5 must learn every day to close the window

PyQt5 must learn every day to pop up a message box



The above is the detailed content of Checkboxes with labels that you must learn every day in PyQt5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template