如何使标签和盒子完全隐藏而不只是半途而废?
隐藏的小部件
非隐藏小部件
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Code'
self.left = 1400
self.top = 500
self.width = 400
self.height = 168
self.initUI()
self.count = 0
def initUI(self):
self.button = QPushButton('button', self)
self.button.move(7,42)
self.button.clicked.connect(self.test)
self.box = QLineEdit(self)
self.box.setGeometry(5,19,20,20)
self.box.setAlignment(Qt.AlignRight)
self.box.hide()
self.l = QLabel(self)
self.l.setText('Letters:')
self.l.move(26,20)
self.l.hide()
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.show()
def test(self):
if self.count % 2 == 0:
self.box.show()
self.l.show()
elif self.count % 2 == 1:
self.l.hide()
self.box.hide()
self.count += 1
print('Box: ', self.box.isVisible())
print('Label: ', self.l.isVisible())
print()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
我正在尝试获取标签并在第一次单击时显示,然后在第二次单击时隐藏,但是在第一次单击时显示,但是在第二次单击时它隐藏if的一半。