【问题标题】:Hide/Show pictures pressing a button PyQt5隐藏/显示图片按下按钮PyQt5
【发布时间】:2016-01-10 21:45:44
【问题描述】:

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

    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)

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

再次感谢。

【问题讨论】:

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


    【解决方案1】:

    这一行应该是:

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

    【讨论】:

    • 我为这个错误填了一个傻瓜,现在我没有消息错误但软件以有线方式运行,当我点击按钮时其他小部件被隐藏,我将提供更多信息澄清问题。非常感谢@a_manthey_67。
    【解决方案2】:

    我修好了。感谢@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)
    

    现在我在调整大小时遇到​​了问题,但这是一个不同的话题,我会试着弄清楚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 2021-11-27
      • 1970-01-01
      相关资源
      最近更新 更多