Search code examples
pyqt5

Dynamically change the visibility of a QLabel and QPushButton when a specific action is triggered


I'm trying to change QLabel and QPushButton visibility when QLineEdit has one index or more, so I don't know what the conditionin of the if statment and how to make the QLabel and QPushButton appears dynamically without emits an action,

the code :

    import PyQt5
    import sys 
    from PyQt5 import QtWidgets
    from PyQt5 import QtCore , QtGui , uic
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import QPropertyAnimation , Qt 
    
    
    class Ui(QWidget):
        def __init__(self):
            super(Ui , self).__init__()
    
            uic.loadUi("login_page.ui" , self)
            
            self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
            self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
            self.show()
            
            
            self.on_Run()
    
    
            
            
        
        
        
        def on_Run(self):
            
            self.label.setVisible(False)
            self.Hide_Show_Password.setVisible(False)
    
    
        
        def show_hide_pass(self):
            #Below Code For Hide and Show the password
    
             if self.Password.text() == :
                self.label.setVisible(True)
                self.Hide_Show_Password.setVisible(True)

Solution

  • In the example below I create a generic button, label, and line edit, and add them to a layout. Then I connect the QLineEdit.textChanged signal to your show_hide_pass method which sends the text contents of the widget each time the contents are edited.

    I am not 100% sure if you were saying that you only wanted the widgets to be visible if there was at least 1 digit, or if the number 1 appeared in the line edit, so I just chose the former, and am checking the length of the text parameter in the if statement, but if you are wanting to check for the number one you can just change that line to if "1" in text:. instead.

    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    
    class Ui(QWidget):
    
        def __init__(self):
            super(Ui , self).__init__()
            self.setAttribute(Qt.WA_TranslucentBackground)
            self.setWindowFlags(Qt.FramelessWindowHint)
    
            # Add Layout and Widgets
            self.layout = QVBoxLayout(self)
            self.label = QLabel("Label")
            self.layout.addWidget(self.label)
            self.Password = QLineEdit()
            self.layout.addWidget(self.Password)
            self.Hide_Show_Password = QPushButton("HideShowPasswordButton")
            self.layout.addWidget(self.Hide_Show_Password)
    
            # Connect widget signals to they slot methds
            self.Password.textChanged.connect(self.show_hide_pass)
            self.on_Run()
    
        def on_Run(self):
            self.label.setVisible(False)
            self.Hide_Show_Password.setVisible(False)
    
        def show_hide_pass(self, text):
             if len(text) > 0:
                self.label.setVisible(True)
                self.Hide_Show_Password.setVisible(True)
    
    
    app = QApplication([])
    window = Ui()
    window.show()
    app.exec_()