QLabel text is not changed in PyQ5?

Viewed 97

I am using this code, in QLineEdit, textChanged.connect() function block . If the length of the text box is greater than one, all labels will display relevant and correct data, but if the length of the text box is Equal to 0 (clear, textbox text by pressing backspace), nothing will happen. I noticed "Enter block" and "End Block" messages(print statements) in screen. All label text remains blank. Try with adjustSize(), show(), resize() also.

if len(self.textbox_search.text()) >= 1:
    self.label_master_count.setText(str(len(self.listbox_master)))
    self.label_starts_count.setText(str(len(self.item_startswith)))
    self.label_contains_count.setText(str(len(self.item_contains)))
    self.label_ends_count.setText(str(len(self.item_endswith)))
else:
    print("Enter Blocks")
    
    self.label_starts_count.setText("9999")
    self.label_starts_count.adjustSize()
    self.label_starts_count.show()
    self.label_contains_count.setText("...")
    self.label_ends_count.setText("...")

    print("End blocks")
1 Answers

I really can't figure out a lot from your code snippet, see here about how to post Question on SO:

How do I ask a good question? and the Include just enough code to allow others to reproduce the problem. For help with this, read How to create a Minimal, Complete, and Verifiable example.

I tried to reproduce your problem, have a look at my code and see if it behaves like your one (wrongly or not). Just let me know about it, I am trying to learn too

from PyQt5 import QtWidgets, QtCore

# from PyQt5 import QtCore, QtGui

import sys
  
  
class Window(QtWidgets.QMainWindow):
  
    def __init__(self):
        super().__init__()
        self.setWindowTitle("PyQt5 QMainWindow ")

        self.setGeometry(100, 100, 500, 400)
        
        self.setStyleSheet("""Window {background : blue}""")
        

        self.lineEdit = QtWidgets.QLineEdit(self)
        self.lineEdit.setGeometry(QtCore.QRect(140, 60, 181, 71))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setPlaceholderText("   Enter your text")
        
        self.label = QtWidgets.QLabel(self)
        self.label.setGeometry(QtCore.QRect(200, 200, 111, 41))
        self.label.setObjectName("label")
        self.label.setText('label')
        
        self.lineEdit.textChanged.connect(self.text_changed)
        
        self.lineEdit.editingFinished.connect(self.enterPress)
        
        
        self.pb = QtWidgets.QPushButton(self)
        self.pb.setObjectName("connect")
        self.pb.setText("RESET")
        self.pb.clicked.connect(self.button_click)
        self.pb.setGeometry(QtCore.QRect(160, 300, 111, 41))
        self.pb.setStyleSheet("background-color : yellow")
        # self.pb.setStyleSheet("background-color : yellow")
        self.pb.setStyleSheet("""
        QPushButton {
            background-color: yellow; 
            border: 2px solid black;
        }
        QPushButton:hover {
            background-color: green;
        }
        """)
        
        self.cnt = 0
        self.reset = False
        
    def text_changed(self, text):

        
        self.cnt = len(self.lineEdit.text()) 
        if len(self.lineEdit.text()) >= 1:
            print("Text changed...", self.cnt)
            print('...')
            self.cnt += 1
        else:
            if self.reset == True:
                self.reset = False
                self.label.setText("label")
                self.setStyleSheet("""Window {background : blue}""")
            
            else:
                self.lineEdit.setText('cleared text')
                self.label.setText("9999")
                self.setStyleSheet("""Window {background : red}""")
        
    def enterPress(self):
        print("Text is now ...")
        self.text= self.lineEdit.text()
        print('  --> ', self.text)
        # print(len(self.lineEdit.text()))
        
    def button_click(self):
        self.reset = True
        self.lineEdit.setText(None)
        



if __name__ == "__main__":
    
    app = QtWidgets.QApplication(sys.argv)

    window = Window()
    window.show()
    
    sys.exit(app.exec_())

In my code QLineEdit starts empty, you can write in in when it reaches len == 0 the label below it changes text

Related