python GUI library graphical interface development PyQt5 check box control QCheckBox detailed use methods and examples
Common methods in the QCheckBox class are tables
method | Description |
---|---|
setChecked() | Set the status of the check box. If it is set to True, it is selected. If it is set to False, it is desselected |
setText() | Sets the display text of the check box |
text() | Returns the display text for the check box |
isChecked() | Check that the check box is selected |
setTriState() | Set the check box to a three-state check box |
setCheckState() | The status Settings of the three-state check box can be seen in the following table |
The three states of the three-state check box
name | value | implication |
---|---|---|
Qt.Checked | 2 | Component not selected (default) |
Qt.PartiallyChecked | 1 | The component is partially selected |
Qt.Unchecked | 0 | Component selected |
Example of the QCheckBox button
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt class CheckBoxDemo(QWidget): def __init__(self, parent=None): super(CheckBoxDemo, self).__init__(parent) # Create a groupBox groupBox = QGroupBox("Checkboxes") groupbox.setflat (False) # Create checkbox 1 and check it by default. Signal trigger events when the state changes self. CheckBox1 = QCheckBox (" & checkBox1 ") self. CheckBox1. SetChecked (True) self.checkBox1.stateChanged.connect(lambda: self.btnstate(self.checkBox1)) # Create a check box, Tag states change signal trigger self. CheckBox2 = QCheckBox (" checkBox2 ") self. CheckBox2. Toggled. Connect (lambda: self.btnstate(self.checkBox2)) # Create check box 3, set the status to 3, set the default selected status to semi-selected, Signal trigger events when the state changes self. CheckBox3 = QCheckBox (" tristateBox ") self. CheckBox3. SetTristate (True) self.checkBox3.setCheckState(Qt.PartiallyChecked) self.checkBox3.stateChanged.connect(lambda: self.btnstate(self.checkBox3)) # Horizontal layout = QHBoxLayout() # Add control to horizontal layout Layout.addWidget (self.checkBox1) addWidget(self.checkBox2) Layout. addWidget(self.checkBox3) # Set the layout of the QGroupBox group. groupBox.setLayout(layout) mainLayout = QVBoxLayout() #QgroupBox controls added to the main interface layout MainLayout.addWidget (groupBox) # Set the main interface layout self.setLayout(mainLayout) # Set the main interface title self.setWindowTitle("checkbox demo") # Output the current status of three check boxes, 0 selected, 1 half selected, If def btnstate(self, btn) is not selected, chk1Status = self.checkBox1.text() + ", isChecked=" + str(self.checkBox1.isChecked()) + ', chekState=' + str( self.checkBox1.checkState()) + "\n" chk2Status = self.checkBox2.text() + ", isChecked=" + str(self.checkBox2.isChecked()) + ', checkState=' + str( self.checkBox2.checkState()) + "\n" chk3Status = self.checkBox3.text() + ", isChecked=" + str(self.checkBox3.isChecked()) + ', checkState=' + str( self.checkBox3.checkState()) + "\n" print(chk1Status + chk2Status + chk3Status) if __name__ == '__main__': app = QApplication(sys.argv) checkboxDemo = CheckBoxDemo() checkboxDemo.show() sys.exit(app.exec_())
The result is shown below
QCheckBox code analysis:
In this example, the three check boxes are added to a horizontal layout manager and to a QGroupBox group
groupBox = QGroupBox("Checkboxes") groupBox.setFlat(False)
Attach the stateChanged signal from all three check boxes to the slot function stateChanged (), passing the object to the slot function using landba mode
Emits the stateChanged signal when the QCheckBox state changes, and triggers the custom slot function btnstate () when the signal changes.
self.checkBox1.stateChanged.connect(lambda: self.btnstate(self.checkBox1)) self.checkBox2.toggled.connect(lambda: self.btnstate(self.checkBox2)) self.checkBox3.stateChanged.connect(lambda: self.btnstate(self.checkBox3))
Instantiate the CheckBox1 and CheckBox2 objects, set the CheckBox1 status to checked, set the CheckBox1 shortcut key, and use the '&' symbol to select the checkbox1 checkbox using the shortcut key Alt+C
self.checkBox1 = QCheckBox("&Checkbox1") self.checkBox1.setChecked(True)
Using the button's isChecked () method to determine whether the check box is selected, its core code is:
chk1Status = self.checkBox1.text() + ", isChecked=" + str(self.checkBox1.isChecked()) + ', chekState=' + str( self.checkBox1.checkState()) + "\n"
Instantiate an object of the QCheckBox class, checkBox3, then use setTristate () to turn on the tri-state mode, then set it to semi-selected and connect the slot function
self.checkBox3 = QCheckBox("tristateBox") self.checkBox3.setTristate(True) self.checkBox3.setCheckState(Qt.PartiallyChecked) self.checkBox3.stateChanged.connect(lambda: self.btnstate(self.checkBox3))
This article explains in detail the PyQt5 check box control QCheckBox detailed use methods and examples, more about PyQt5 control knowledge please see the following links
- python GUI library graphical interface development PyQt5 layout control QHBoxLayout detailed use methods and examples
- python GUI library graphical interface development of PyQt5 window layout control QStackedWidget detailed use method
- Pyqt5 Adaptive layout example
- Discussion on the layout of pyqt5 in QMainWindow
- PyQt5 must learn layout management every day
- python GUI library graphical interface development PyQt5 scrollbar control QScrollBar detailed use methods and examples
- python GUI library graphical interface development PyQt5 MDI(Multi-document window)QMidArea detailed use methods and examples
- python GUI library graphical interface development of PyQt5 tree structure control QTreeWidget detailed use methods and examples
- python GUI library graphical interface development PyQt5 TAB control QTabWidget detailed use methods and examples
- python GUI library graphical interface development PyQt5 table control QTableView detailed use methods and examples
- python GUI library graphical interface development PyQt5 print control QPrinter detailed use methods and examples
- python GUI library graphical interface development PyQt5 status bar control QStatusBar detailed use method example
- python GUI library graphical interface development PyQt5 counter control QSpinBox detailed use methods and examples
- python GUI library graphical interface development PyQt5 switch button control QPushButton detailed use methods and examples
- python GUI library graphical interface development PyQt5 multi-line text box control QTextEdit detailed use method example
- python GUI library graphical interface development PyQt5 radio button control QRadioButton detailed use method and example
- python GUI library graphical interface development PyQt5 layout control QVBoxLayout detailed use methods and examples
Related article
-
How does Python get the size and duration of a video file
This article mainly introduces in detail how Python achieves the size and length of the video file, the example code in the article explains in detail, interested partners can follow Xiaobian together to understand2023-03-03 -
Use Python to climb the best Universities network University rankings
This article mainly introduces how to use Python to climb the best university network university ranking, has a certain reference value, interested partners can refer to it2018-02-02 -
Analysis and resolution of memory leakage caused by improper use of Python local cache
Recently, I encountered a memory leak problem caused by improper use of local cache in the project development, so this article mainly analyzes the causes of the problem and has been solved2023-08-08 -
How to migrate your application to Python3 in three steps
This article mainly introduces how to migrate your application to Python3 three steps, the article through the example code introduction is very detailed, for everyone's study or work has a certain reference learning value, need friends to follow the small series to learn together2019-12-12 -
Example code for implementing the Python3 online editor using CodeMirror
This article mainly introduces the use of CodeMirror to implement Python3 online editor example code, the article through the example code introduction is very detailed, for everyone's study or work has a certain reference learning value, need friends to follow the small series to learn together2019-01-01 -
Details of the performance and multi-process of logging module in python
When using Python to write background tasks, it is often necessary to use output logs to record the running status of the program, and save the details of the error when it occurs for debugging and analysis. Python's logging module is a good helper in this case. This article introduces the python logging module performance and multi-process related information, need friends can refer to the next.2017-07-07 -
Examples of django creating filters
This article mainly introduces django to create filter examples detailed information, mainly explains django to create filters to unify the processing of strings, need friends can refer to the next2017-08-08 -
How do I deploy a python project on a server
Server less technology is a way to run applications without managing servers. The most popular server less platform is AWS Lambda. This article mainly introduces how to deploy python projects on a server2023-10-10 -
A collection of common Python methods and tips
This article mainly introduces the collection of some Python common methods and skills, this article explains the three methods of reversing the string, the four methods of traversing the dictionary, the three methods of traversing the list, the dictionary sorting method and other Python common skills and methods, the need of friends can refer to the next2015-05-05 -
The clever use of the split() function when reading txt files in python
This article mainly introduces the split() function in python when reading txt files, has a good reference value, I hope to help you. If there are mistakes or incomplete areas, please feel free to comment2022-11-11
Latest comments