Python 为什么可以';我是否将三态QCheckBox设置为Qt.PartiallyChecked并使QCheckBox显示partial check(部分检查)?
我正在使用python3和PyQt5 下面的示例有三个QpushButton和一个将setTristate设置为true的QCheckBox。 我可以直接点击复选框,它会在这三个状态之间循环 问题是:如果我试图从相邻的按钮设置状态值,它只会给我检查或取消检查。真让人恼火Python 为什么可以';我是否将三态QCheckBox设置为Qt.PartiallyChecked并使QCheckBox显示partial check(部分检查)?,python,python-3.x,pyqt5,qcheckbox,Python,Python 3.x,Pyqt5,Qcheckbox,我正在使用python3和PyQt5 下面的示例有三个QpushButton和一个将setTristate设置为true的QCheckBox。 我可以直接点击复选框,它会在这三个状态之间循环 问题是:如果我试图从相邻的按钮设置状态值,它只会给我检查或取消检查。真让人恼火 #!/usr/bin/env python3 from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * import
#!/usr/bin/env python3
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
qb1=QPushButton("Uncheck it")
qb2=QPushButton("Partial Check it")
qb3=QPushButton("Check it reals")
qb1.clicked.connect(self.uncheck_it)
qb2.clicked.connect(self.partial_check_it)
qb3.clicked.connect(self.check_it)
self.qch=QCheckBox("Chonk")
self.qch.stateChanged.connect(self.chonker)
self.qch.setTristate(True)
hbox.addWidget(self.qch)
hbox.addWidget(qb1)
hbox.addWidget(qb2)
hbox.addWidget(qb3)
self.setLayout(hbox)
self.move(300, 200)
self.setWindowTitle('Choooo')
self.show()
def chonker(self):
print("chonker", self.sender().text(),self.sender().isChecked())
def uncheck_it(self):
self.qch.setChecked(Qt.Unchecked)
print ("Uncheck it")
def partial_check_it(self):
self.qch.setChecked(Qt.PartiallyChecked)
print ("Partial check it")
def check_it(self):
self.qch.setChecked(Qt.Checked)
print ("Check it")
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
该方法仅更改为两种状态:已选中或未选中。如果要更改为所有3种状态,则必须使用以下方法:
def取消选中它(self):
self.qch.setCheckState(Qt.Unchecked)
打印(“取消选中”)
def部分检查(自身):
self.qch.setCheckState(Qt.PartiallyChecked)
打印(“部分检查”)
def检查(自我):
self.qch.setCheckState(Qt.Checked)
打印(“检查”)
谢谢用户eyllanesc
在下面的代码中,使用了适用于三态复选框的替代功能
将函数setCheckState替换为函数setCheckState。
替换的函数使用实际返回三态值的函数checkState进行检查
这个有效
#!/usr/bin/env python3
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
qb1=QPushButton("Uncheck it")
qb2=QPushButton("Partial Check it")
qb3=QPushButton("Check it reals")
qb1.clicked.connect(self.uncheck_it)
qb2.clicked.connect(self.partial_check_it)
qb3.clicked.connect(self.check_it)
self.qch=QCheckBox("Chonk")
self.qch.stateChanged.connect(self.chonker)
self.qch.setTristate(True)
hbox.addWidget(self.qch)
hbox.addWidget(qb1)
hbox.addWidget(qb2)
hbox.addWidget(qb3)
self.setLayout(hbox)
self.move(300, 200)
self.setWindowTitle('Choooo')
self.show()
def chonker(self):
print("chonker", self.sender().text(),self.sender().checkState())
def uncheck_it(self):
self.qch.setCheckState(Qt.Unchecked)
print ("Uncheck it")
def partial_check_it(self):
self.qch.setCheckState(Qt.PartiallyChecked)
print ("Partial check it")
def check_it(self):
self.qch.setCheckState(Qt.Checked)
print ("Check it")
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
我不明白为什么要发布第二个“答案”,这已经涵盖了我的答案。我会理解一个新的答案,如果它给出了另一个选择,或者指出其他一些答案有缺点。也许我错了,你能解释一下吗?你的回复给网站带来了什么好处?我打字时还没有看到上一个。没有必要把它抹掉。谢谢
#!/usr/bin/env python3
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
qb1=QPushButton("Uncheck it")
qb2=QPushButton("Partial Check it")
qb3=QPushButton("Check it reals")
qb1.clicked.connect(self.uncheck_it)
qb2.clicked.connect(self.partial_check_it)
qb3.clicked.connect(self.check_it)
self.qch=QCheckBox("Chonk")
self.qch.stateChanged.connect(self.chonker)
self.qch.setTristate(True)
hbox.addWidget(self.qch)
hbox.addWidget(qb1)
hbox.addWidget(qb2)
hbox.addWidget(qb3)
self.setLayout(hbox)
self.move(300, 200)
self.setWindowTitle('Choooo')
self.show()
def chonker(self):
print("chonker", self.sender().text(),self.sender().checkState())
def uncheck_it(self):
self.qch.setCheckState(Qt.Unchecked)
print ("Uncheck it")
def partial_check_it(self):
self.qch.setCheckState(Qt.PartiallyChecked)
print ("Partial check it")
def check_it(self):
self.qch.setCheckState(Qt.Checked)
print ("Check it")
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()