Saverio Vasapollo
Saverio Vasapollo

Reputation: 53

Hide/Show pictures pressing a button PyQt5

I have a toolbar with a button that should enable/disable a label that shows some pictures, how do I do this? I created a function that I call for doing this, but when I run the code I get this:

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

And here some code:

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....]

Thank you to everybody.


I fixed the line showHidePicture.triggered.connect(self.tbody.showPictures()) that I'm leaving in case other people need, and I don't have the error but the software behaves in a wired way, when I click it shows/hides those other widgets:

    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)

so, I still need your help guys ;)

Thanks again.

Upvotes: 0

Views: 972

Answers (2)

Saverio Vasapollo
Saverio Vasapollo

Reputation: 53

I fixed it. Thanks to @a_manthey_67 for the first part: showHidePicture.triggered.connect(self.tbody.showPictures)

and for the second problem, I had to add the line self.label_picture.setParent(None) to the function showPictures, like this:

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)

Now I'm having a problem with the resize but it's a different topic, I'll try to figure out.

Upvotes: 0

a_manthey_67
a_manthey_67

Reputation: 4286

this line should be:

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

Upvotes: 1

Related Questions