python - 触发和更新停靠窗口

标签 python python-2.7 pyqt pyqt4

我正在尝试使用 GUI 控制一些阀门,并在按下按钮时更新显示。以下代码产生正确的初始显示,但是当我单击其中一个复选框时,我总是得到 i=2 的值,而未选中时,我得到 i=0 。我也不知道如何更新停靠窗口,以便消息从 close 切换到 open,反之亦然。我确实看到数组中 self.states 的结果值发生了变化,但总是留下 ['open', 'close', 'open', 'close', 'close' , 'close'] 虽然这已更改,但屏幕右侧的红色 close 并未更改为 `open

class ApplicationWindow(gui.QMainWindow):
    def __init__(self):
        self.nCheckBoxes=6
        self.states = ['closed']*self.nCheckBoxes

        gui.QMainWindow.__init__(self)
        self.setAttribute(core.Qt.WA_DeleteOnClose)
        self.setWindowTitle("PiView")
        self.file_menu = gui.QMenu('&File', self)
        self.file_menu.addAction('&Quit', self.fileQuit, core.Qt.CTRL + core.Qt.Key_Q)
        self.menuBar().addMenu(self.file_menu)
        self.help_menu = gui.QMenu('&Help', self)
        self.menuBar().addSeparator()
        self.menuBar().addMenu(self.help_menu)
        self.help_menu.addAction('&About', self.about)
        self.help_menu.addAction('&Docs', self.doc)
        self.main_widget = gui.QWidget(self)

        l = gui.QVBoxLayout(self.main_widget)
        self.dc = MyMplCanvas(self.main_widget, width=5, height=4, dpi=100)
        l.addWidget(self.dc)

        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)

        self.dw = self.createDockWindows(self.dc)
        self.statusBar().showMessage("Initialized", 2000)

    def createDockWindows(self, MyMplCanvas):
        """
        Create all the dock widgets
        """
        self.drawValves()
        self.drawGraphAdjustments()
        self.drawStartAndStop()

    def drawValves(self):
        cboxes = gui.QDockWidget("Controls", self)
        cboxes.setAllowedAreas(core.Qt.LeftDockWidgetArea)
        w = gui.QWidget()
        #layout = gui.QVBoxLayout()
        layout = gui.QGridLayout()
        w.setLayout(layout)

        self.c = [0]*self.nCheckBoxes
        # Create self.nCheckBoxes
        msgBox = gui.QLabel()
        msgBox.setText("States")
        font = gui.QFont()
        font.setBold(True)
        msgBox.setFont(font)
        msgBox.setStyleSheet("color: rgb(255,0,0)")
        layout.addWidget(msgBox,0,1)
        for i in range(self.nCheckBoxes):
            self.c[i] = gui.QCheckBox("Valve " + str(i))
            self.c[i].setChecked(False)
            #self.c[i].stateChanged.connect(lambda:self.btnstate(self.c[i]))
            #self.c[i].stateChanged.connect(self.checkedBox)
            self.c[i].stateChanged.connect(lambda i: self.checkedBox(i))
            layout.addWidget(self.c[i],i+1,0)
            # Messages
            msgBox = gui.QLabel()
            msgBox.setText(self.states[i])
            if self.states[i] == 'closed':
                msgBox.setStyleSheet("color: rgb(255,0,0)")
            else:
                msgBox.setStyleSheet("color: rgb(0,255,0)")
            layout.addWidget(msgBox,i+1,1)

        spacerItem = gui.QSpacerItem(20,40, gui.QSizePolicy.Minimum, gui.QSizePolicy.Expanding)
        layout.addItem(spacerItem)
        cboxes.setWidget(w)
        self.addDockWidget(core.Qt.LeftDockWidgetArea, cboxes)

    def checkedBox(self,i):
        sender = self.sender()
        self.states[i] = 'open' if 'close' else 'close'
        # For debugging
        print i
        print self.states

如果我理解正确的话,我可以像通常在函数 checkedBox 中一样启动阀门。

这是显示的内容 Unchecked Checked 我希望能够单击右侧的复选框和消息,将“关闭”更改为“打开”,反之亦然。

最佳答案

出现问题的原因是 i 的默认值采用信号发送的值(即状态:Unchecked = 0、PartiallyChecked = 1、Checked = 2),为了解决这个问题,我们将向函数添加另一个值:复选框的索引。另外,如果您想更改 QLabel 的文本,您必须能够访问它们,因此我创建了一个带有标签的新列表。为了进行更改,我们将使用状态列表,为此实现函数 updateLabels

class ApplicationWindow(gui.QMainWindow):
    def __init__(self):
        self.nCheckBoxes=6
        self.states = ['closed']*self.nCheckBoxes

        gui.QMainWindow.__init__(self)
        self.setAttribute(core.Qt.WA_DeleteOnClose)
        self.setWindowTitle("PiView")
        self.file_menu = gui.QMenu('&File', self)
        #self.file_menu.addAction('&Quit', self.fileQuit, core.Qt.CTRL + core.Qt.Key_Q)
        self.menuBar().addMenu(self.file_menu)
        self.help_menu = gui.QMenu('&Help', self)
        self.menuBar().addSeparator()
        self.menuBar().addMenu(self.help_menu)
        #self.help_menu.addAction('&About', self.about)
        #self.help_menu.addAction('&Docs', self.doc)
        self.main_widget = gui.QWidget(self)

        l = gui.QVBoxLayout(self.main_widget)
        self.dc = MyMplCanvas(self.main_widget, width=5, height=4, dpi=100)
        l.addWidget(self.dc)

        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)

        self.dw = self.createDockWindows(self.dc)
        self.statusBar().showMessage("Initialized", 2000)

    def createDockWindows(self, MyMplCanvas):
        """
        Create all the dock widgets
        """
        self.drawValves()
        #self.drawGraphAdjustments()
        #self.drawStartAndStop()

    def drawValves(self):
        cboxes = gui.QDockWidget("Controls", self)
        cboxes.setAllowedAreas(core.Qt.LeftDockWidgetArea)
        w = gui.QWidget()
        #layout = gui.QVBoxLayout()
        layout = gui.QGridLayout()
        w.setLayout(layout)

        self.c = []
        self.l = []
        # Create self.nCheckBoxes
        msgBox = gui.QLabel()
        msgBox.setText("States")
        font = gui.QFont()
        font.setBold(True)
        msgBox.setFont(font)
        msgBox.setStyleSheet("color: rgb(255,0,0)")
        layout.addWidget(msgBox,0,1)
        for i in range(self.nCheckBoxes):
            checkBox = gui.QCheckBox("Valve " + str(i))
            checkBox.setChecked(False)
            checkBox.stateChanged.connect(lambda state, p=i: self.checkedBox(state, p))
            self.c.append(checkBox)

            layout.addWidget(self.c[i],i+1,0)
            msgBox = gui.QLabel()
            msgBox.setText(self.states[i])
            self.l.append(msgBox)
            layout.addWidget(msgBox,i+1,1)

        self.update_labels()

        spacerItem = gui.QSpacerItem(20,40, gui.QSizePolicy.Minimum, gui.QSizePolicy.Expanding)
        layout.addItem(spacerItem)
        cboxes.setWidget(w)
        self.addDockWidget(core.Qt.LeftDockWidgetArea, cboxes)

    def checkedBox(self, state, p):
        if state == core.Qt.Unchecked:
            self.states[p] = 'closed'
        else:
            self.states[p] = 'open'

        self.update_labels()

    def update_labels(self):
        for i in range(self.nCheckBoxes):
            if self.states[i] == 'closed':
                text = "closed"
                styleSheet = "color: rgb(255,0,0)"
            else:
                text = "open"
                styleSheet = "color: rgb(0,255,0)"
            self.l[i].setText(text)
            self.l[i].setStyleSheet(styleSheet)

enter image description here

enter image description here

关于python - 触发和更新停靠窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43145460/

相关文章:

python - 使用 node.js crypto aes256 加密并使用 python2.7 PyCrypto 解密

python - 从python中的两个字符串创建变量名

python - PyQt 中的 QGraphicsItem 什么时候被销毁?

python - 我正在使用 Peewee,我需要为我的数据库创建迁移文件

python - 主窗口的多个垂直工具栏

python - 如何在 Process() 中中断 Queue.get()?

python - 无法使用请求从网页中抓取某个字段的值

python - conda info 中声明的 python 版本与 conda list 中给出的 python 版本有什么区别?

python - 如何在python中将变量传递给argparse

Python如何根据文件内容创建UUID