Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Eclipse Python3 PyQt4创建简单的QCheckBox并更改布尔变量_Eclipse_Python 3.x_Pyqt4_Windows 7 X64_Qcheckbox - Fatal编程技术网

Eclipse Python3 PyQt4创建简单的QCheckBox并更改布尔变量

Eclipse Python3 PyQt4创建简单的QCheckBox并更改布尔变量,eclipse,python-3.x,pyqt4,windows-7-x64,qcheckbox,Eclipse,Python 3.x,Pyqt4,Windows 7 X64,Qcheckbox,因此,我一直在尝试使用Python3.3和PyQt4编写GUI。我已经学习了一些教程,但仍然不知道如何在全局变量中选中和取消选中触发更改的复选框。我不能让它触发任何事情,因为所有的教程使用的方法,不会为我工作 这个程序太大了,不能在这里作为一个整体复制,所以我把程序的基本结构放在了复选框周围 import sys from PyQt4 import QtGui, QtCore ILCheck = False class SelectionWindow(QtGui.QWidget):

因此,我一直在尝试使用Python3.3和PyQt4编写GUI。我已经学习了一些教程,但仍然不知道如何在全局变量中选中和取消选中触发更改的复选框。我不能让它触发任何事情,因为所有的教程使用的方法,不会为我工作

这个程序太大了,不能在这里作为一个整体复制,所以我把程序的基本结构放在了复选框周围

import sys
from PyQt4 import QtGui, QtCore

ILCheck = False

class SelectionWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(SelectionWindow, self).__init__(parent)

        ILCheckbox = QtGui.QCheckBox(self)
        ILCheckbox.setCheckState(QtCore.Qt.Unchecked)

        MainLayout = QtGui.QGridLayout()
        MainLayout.addWidget(ILCheckbox, 0, 0, 1, 1)
        self.setLayout(MainLayout)
这就是我被困的地方。我想做的是,如果选中了ILCheckbox,则将ILCheck的状态更改为True,如果未选中ILCheckbox,则将其更改回False。我们花了差不多一整天的时间来研究这个问题,但没有一个教程对我们有多大帮助。

当复选框的状态真的发生变化时,它会发出一个stateChanged事件!。将其连接到事件处理程序:

import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class SelectionWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.ILCheck = False

        ILCheckbox = QCheckBox(self)
        ILCheckbox.setCheckState(Qt.Unchecked)

        ILCheckbox.stateChanged.connect(self.ILCheckbox_changed)

        MainLayout = QGridLayout()
        MainLayout.addWidget(ILCheckbox, 0, 0, 1, 1)

        self.setLayout(MainLayout)

    def ILCheckbox_changed(self, state):
        self.ILCheck = (state == Qt.Checked)

        print(self.ILCheck)


if __name__ == '__main__':
  app = QApplication(sys.argv)
  window = SelectionWindow()

  window.show()
  sys.exit(app.exec_())
当其状态发生更改时,复选框将发出stateChanged事件!。将其连接到事件处理程序:

import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class SelectionWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.ILCheck = False

        ILCheckbox = QCheckBox(self)
        ILCheckbox.setCheckState(Qt.Unchecked)

        ILCheckbox.stateChanged.connect(self.ILCheckbox_changed)

        MainLayout = QGridLayout()
        MainLayout.addWidget(ILCheckbox, 0, 0, 1, 1)

        self.setLayout(MainLayout)

    def ILCheckbox_changed(self, state):
        self.ILCheck = (state == Qt.Checked)

        print(self.ILCheck)


if __name__ == '__main__':
  app = QApplication(sys.argv)
  window = SelectionWindow()

  window.show()
  sys.exit(app.exec_())

尽量避免使用全局变量

相反,将复选框作为窗口的属性,并直接测试其状态:

class SelectionWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(SelectionWindow, self).__init__(parent)
        self.ILCheckbox = QtGui.QCheckBox(self)
        self.ILCheckbox.setChecked(QtCore.Qt.Unchecked)
        MainLayout = QtGui.QGridLayout()
        MainLayout.addWidget(self.ILCheckbox, 0, 0, 1, 1)
        self.setLayout(MainLayout)
...

window = SelectionWindow()
print window.ILCheckbox.isChecked()

尽量避免使用全局变量

相反,将复选框作为窗口的属性,并直接测试其状态:

class SelectionWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(SelectionWindow, self).__init__(parent)
        self.ILCheckbox = QtGui.QCheckBox(self)
        self.ILCheckbox.setChecked(QtCore.Qt.Unchecked)
        MainLayout = QtGui.QGridLayout()
        MainLayout.addWidget(self.ILCheckbox, 0, 0, 1, 1)
        self.setLayout(MainLayout)
...

window = SelectionWindow()
print window.ILCheckbox.isChecked()

谢谢但这很奇怪,因为eclipse在状态更改后没有给我.connect选项。我觉得这有点奇怪,谢谢。但这很奇怪,因为eclipse在状态更改后没有给我.connect选项。我只是觉得有点奇怪。