按下按钮PyQt5隐藏/显示图片

时间:2016-01-10 21:45:44

标签: image python-3.x qt5 picturebox pyqt5

我有一个带有按钮的工具栏,可以启用/禁用显示某些图片的标签,我该怎么办? 我创建了一个函数,我打算这样做,但是当我运行代码时,我得到了这个:

    File "saw_notes_027.py", line 41, in initUI
        showHidePicture.triggered.connect(self.tbody.showPictures())
    TypeError: argument 1 has unexpected type 'NoneType'

这里有一些代码:

class AppBase(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.tbody = Body(self)
        self.setCentralWidget(self.tbody)

        showHidePicture = QAction(QIcon('image20151027_123417794.jpg'), 'Show / Hide Picture', self)
        showHidePicture.setShortcut('Ctrl+I')
        showHidePicture.setStatusTip('Show / Hide Picture')
        showHidePicture.triggered.connect(self.tbody.showPictures())

        [...other code....]

        toolbar = self.addToolBar('Picture')
        toolbar.addAction(showHidePicture)


class Body(QWidget):

    def __init__(self, parent):
        super().__init__(parent)
        self.initBody()

    def initBody(self):

    [...other code....]

    self.grid.addWidget(self.label_exercise, 0, 0)
    self.grid.addWidget(self.combo_exercise, 1, 0)
    self.grid.addWidget(self.label_key, 0, 1)
    self.grid.addWidget(self.combo_key, 1, 1)
    self.grid.addWidget(self.label_font, 0, 2)
    self.grid.addWidget(self.btn_font, 1, 2)
    self.grid.addWidget(self.sld, 2, 0, 1, 3)
    self.grid.addWidget(self.lcd, 2, 3)
    self.grid.addWidget(self.label_note, 3, 0)
    self.grid.addWidget(self.label_string, 3, 1)
    self.grid.addWidget(self.label_finger, 3, 2)
    self.grid.addWidget(self.btn_start, 4, 0)
    self.grid.addWidget(self.btn_exit, 4, 1)
    self.grid.addWidget(self.label_picture, 5, 0, 1, 6)
    self.setLayout(self.grid)

    self.active_pictures = False

    def showPictures(self):
        if self.active_pictures == False:
            self.active_pictures = True
            self.grid.addWidget(self.label_picture, 5, 0, 1, 3)
        else:
            self.active_pictures = False
            self.grid.removeWidget(self.label_picture)
    [...other code....]

谢谢大家。

我修复了我要离开的行showHidePicture.triggered.connect(self.tbody.showPictures()),以防其他人需要,我没有错误,但软件的行为是有线的,当我点击它显示/隐藏其他小部件:

    self.grid.addWidget(self.label_note, 3, 0)
    self.grid.addWidget(self.label_string, 3, 1)
    self.grid.addWidget(self.label_finger, 3, 2)
    self.grid.addWidget(self.btn_start, 4, 0)
    self.grid.addWidget(self.btn_exit, 4, 1)

所以,我仍然需要你的帮助;)

再次感谢。

2 个答案:

答案 0 :(得分:1)

这一行应该是:

showHidePicture.triggered.connect(self.tbody.showPictures)

答案 1 :(得分:0)

我修好了。感谢@ a_manthey_67的第一部分: showHidePicture.triggered.connect(self.tbody.showPictures)

对于第二个问题,我必须将行self.label_picture.setParent(None)添加到showPictures函数中,如下所示:

def showPictures(self):
    if self.active_pictures == False:
        self.active_pictures = True
        self.grid.addWidget(self.label_picture, 5, 0, 1, 6)
    else:
        self.active_pictures = False
        self.grid.removeWidget(self.label_picture)
        self.label_picture.setParent(None)

现在我遇到调整大小的问题,但这是一个不同的主题,我会试着想出来。