bStart和bStop单击函数字面上也是一样的,但bStop函数调用错误。
为什么不能在元组对象上调用.hide()?
from PyQt5.QtWidgets import (QApplication, QFrame, QGridLayout, QHBoxLayout, QPushButton, QSizePolicy, QSpacerItem, QToolButton, QVBoxLayout, QWidget, QLabel)
import frames
class GUI(QWidget):
def __init__(self, parent=None):
super(GUI, self).__init__(parent=parent)
self.principalLayout = QHBoxLayout(self)
self.controlFrame = QFrame(self)
self.gridLayout = QGridLayout(self.controlFrame)
self.principalLayout.addWidget(self.controlFrame)
self.widgets()
print (self.bStart)
print(buttons(self))
def widgets(self):
self.bStart = QPushButton('Start')
self.gridLayout.addWidget(self.bStart, 0, 0)
self.bStart.clicked.connect(lambda: self.hide_layout(self.bStart, self.bStop))
self.bStop = QPushButton('Stop')
self.bStop.clicked.connect(lambda: self.hide_layout(buttons(self)))
self.gridLayout.addWidget(self.bStop, 0, 2)
def hide_layout(self, *args):
for a in args:
a.hide()
def show_layout(self, *args):
for a in args:
a.show()
def buttons(self):
return (self.bStart, self.bStop)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = GUI()
w.show()
sys.exit(app.exec_())
打印输出bStart按钮:
<PyQt5.QtWidgets.QPushButton object at 0x7f089fddac18>
打印输出按钮()功能:
(<PyQt5.QtWidgets.QPushButton object at 0x7f089fddac18>, <PyQt5.QtWidgets.QPushButton object at 0x7f089fddadc8>)
错误bStop按钮点击:
Traceback (most recent call last):
File "gui.py", line 23, in <lambda>
self.bStop.clicked.connect(lambda: self.hide_layout(buttons(self)))
File "gui.py", line 28, in hide_layout
a.hide()
AttributeError: 'tuple' object has no attribute 'hide'
答案 0 :(得分:4)
您忘记解压email@email.com
:
tuple
答案 1 :(得分:1)
c-xc-e
返回元组,然后将其作为一个参数传递给buttons
方法。该方法将每个参数视为要隐藏的对象:
self.hide_layout()
这里for a in args:
a.hide()
是所有参数的序列,args
其中一个参数。既然你直接传入了元组,那就是一个参数。
如果要隐藏元组中包含的对象,请将元组内容应用为单独的参数:
a
或将lambda: self.hide_layout(*buttons(self))
的签名更改为仅接受一个参数,然后将其视为序列:
hide_layout()
同样适用于def hide_layout(self, args): # note, no *
方法。