PyQt5 - 三态复选框
当我们创建一个复选框时,默认情况下有两个状态是选中的,即 True 和其他是 False。但是我们可以在setTristate
方法的帮助下为其添加既不是 True 也不是 False 的第三个状态。
下面是所有三个状态的样子。
Syntax : checkbox.setTristate(True)
Argument : It takes bool as argument.
Action performed : It will add third state to the check box.
下面是实现。
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
# setting title
self.setWindowTitle("Python ")
# setting geometry
self.setGeometry(100, 100, 600, 400)
# calling method
self.UiComponents()
# showing all the widgets
self.show()
# method for widgets
def UiComponents(self):
# creating the check-box
checkbox = QCheckBox('Check Box', self)
# setting geometry of check box
checkbox.setGeometry(200, 150, 100, 30)
# creating the third state
checkbox.setTristate(True)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :