我有一个工具栏,上面有一个按钮,可以启用/禁用显示一些图片的标签,我该怎么做?我创建了一个函数,我调用它来完成这项工作,但当我运行代码时,我得到的是:
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)
所以,我仍然需要你们的帮助;)
再次感谢。
此行应该是:
showHidePicture.triggered.connect(self.tbody.showPictures)
我修复了它。感谢@a_manthey_67的第一部分:showHidePicture.triggered.connect(self.tbody.showPictures)
对于第二个问题,我不得不在函数showPictures中添加self.label_picture.setParent(None)
行,如下所示:
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)
现在我有一个调整大小的问题,但这是一个不同的主题,我会努力解决。