hiding a QLabel at default
-
wrote 16 days ago last edited by prosperrr
am building a car dealership app and id like to know how to hide a qlabel at default until an error occurs it display the label and displays the error
the signup section
self.labelImg = self.findChild(QLabel, 'error_label')
self.labelInp.hide() self.labelImg.hide()
from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.uic import loadUi import sys import sqlite3 import re import os db = sqlite3.connect('CAR.db') exe = db.cursor() exe.execute("""CREATE TABLE IF NOT EXISTS Login( email VARCHAR NOT NULL, role TEXT, password TEXT);""") exe.execute("""CREATE TABLE IF NOT EXISTS Signup( email VARCHAR NOT NULL, role TEXT, firstName TEXT NOT NULL, LastName TEXT NOT NULL, password TEXT);""") class CARLOAD(QDialog): def __init__(self, parent=None): super().__init__(parent) loadUi("VLogin.ui", self) self.loginbutton = self.findChild(QPushButton, 'login_btn') self.signupbutton = self.findChild(QPushButton, 'signup_btn') self.signupbutton.clicked.connect(self.Signup) self.loginbutton.clicked.connect(self.Login) def Login(self): self.hide() login_page = LoginPage() login_page.exec_() def Signup(self): self.hide() signup_page = SignupPage() signup_page.exec_() class SignupPage(QDialog): def __init__(self, parent=None): super().__init__(parent) loadUi('VSignup.ui', self) self.firstname = self.findChild(QLineEdit, 'first_name') self.lastname = self.findChild(QLineEdit, 'last_name') self.password = self.findChild(QLineEdit, 'pass') self.confirm_pass = self.findChild(QLineEdit, 'conf_pass') self.register = self.findChild(QPushButton, 'signup_btn') self.backtologin = self.findChild(QPushButton, 'bck_login') self.labelInp = self.findChild(QLabel, 'error_inp') self.labelImg = self.findChild(QLabel, 'error_label') self.labelInp.hide() self.labelImg.hide() self.register.clicked.connect(self.welcome) self.backtologin.clicked.connect(self.home) def home(self): self.hide() home = CARLOAD() home.exec_() def welcome(self): self.hide() welcome_page = welcomePage() welcome_page.exec_() class LoginPage(QDialog): def __init__(self, parent=None): super().__init__(parent) loadUi('VLogin1.ui', self) self.emailinput = self.findChild(QLineEdit, 'email_inp') self.passwordinput = self.findChild(QLineEdit, 'pass_inp') self.bcksng = self.findChild(QPushButton, 'back_sng') self.loginBtn = self.findChild(QPushButton, 'login_btn') self.bcksng.clicked.connect(self.home) def home(self): self.hide() home = CARLOAD() home.exec_() class welcomePage(QDialog): def __init__(self, parent=None): super().__init__(parent) loadUi('VWelcome.ui', self) self.loader = self.findChild(QProgressBar, 'progressBar') self.update = self.findChild(QLabel, 'update') self.timer = QTimer(self) self.timer.timeout.connect(self.progressUp) self.timer.start(100) def home(self): self.hide() home = CARLOAD() home.exec_() def progressUp(self): current_value = self.loader.value() # Get the current value of the progress bar # Increase the value by 5 if current_value < 100: self.loader.setValue(current_value + 1) # Update the text based on the current progress value if 20 <= current_value < 40: self.update.setText(f'Loading Interface... {current_value}%') elif 40 <= current_value < 60: self.update.setText(f'Loading Components... {current_value}%') elif 60 <= current_value < 80: self.update.setText(f'Almost there... {current_value}%') elif 80 <= current_value < 90: self.update.setText(f'Setting up Dashboard... {current_value}%') # When the progress bar reaches 100, stop the timer and show the completion message elif current_value == 100: self.timer.stop() self.update.setText("Setup Complete! 100%") self.home() # else: # self.timer.stop() # self.progressUp.setText("Setup Complete!") # class CARLOAD(QDialog): # def __init__(self, paren=None): # super().__init__(parent=None) # loadUi("CARLogin.ui", self) # # self.loginbutton = self.findChild(QPushButton, 'login_btn') # # self.loginbutton.clicked.connect(self.welcome) # def welcome(self): # self.hide() # self.welcome_window = WELCOME() # self.welcome_window.show() # class WELCOME(QDialog): # def __init__(self, parent=None): # super().__init__(parent) # loadUi('CARPage1.ui', self) # Running the application if __name__ == "__main__": app = QApplication(sys.argv) window = CARLOAD() window.show() sys.exit(app.exec_())
-
Hi and welcome to devnet,
How are you setting up your label ?
On a side note, I would recommend you take the time to re-evaluate your architecture. Your chain of dialogs is currently pretty brittle.
There are other ways to switch from one widget to another that does not involve creating a new widget every time.
Check for example QStackedWidget.
You might want to also consider QMainWindow which provides a status bar.
Depending on the error you might want to consider a QMessageBox using the appropriate level of urgency to show the message.
2/2