In the python file, I have declared a main window and 2 child pages. Class UserSetup & class CreateID both have set_button_focus function. When I remove the function from CreateID, only then it works in UserSetup. If I keep it in CreateID and load UserSetup. textbox01 is focused. In both instances, when the child page is loaded, I want the button to be focused.
class UserSetup(QWidget):
def __init__(self, parent: "Setup"):
super().__init__(parent)
self.parent: "Setup" = parent # Store the parent reference
self.window_width = 500
self.window_height = 450
self.assets_dir = "Assets" # Assets directory
self.temp_image_path = os.path.join(self.assets_dir, "TempProfile.png")
self.profile_image_path = os.path.join(self.assets_dir, "ProfilePic.png")
# Create the user setup label
label01 = QLabel("USER SETUP", self)
label01.setFont(QFont("Georgia", 25)) # Set font to Georgia, size 25
label01.setStyleSheet("color: #FFDE59;") # Set foreground color
label01.adjustSize() # Adjust the label size according to text width
label01.move((self.window_width - label01.width()) // 2, 10) # Horizontally center and position vertically 10px down
# Create the horizontal line (QFrame)
line = QFrame(self)
line.setFrameShape(QFrame.Shape.HLine) # Set the frame to be a horizontal line
line.setStyleSheet("background-color: #D9D9D9;") # Set line color
line.setGeometry(0, label01.y() + label01.height() + 1, self.window_width, 1) # Position the line 1px below the label
# Load and position the profile image
self.profile_image = QLabel(self)
pixmap = QPixmap(self.temp_image_path)
self.profile_image.setPixmap(pixmap)
self.profile_image.setScaledContents(True) # Scale the image if necessary
self.profile_image.resize(100, 100) # Set label size
self.profile_image.setStyleSheet("border: 1px solid #FFDE59;")
self.profile_image.move((self.window_width - self.profile_image.width()) // 2, label01.y() + label01.height() + 20)
self.profile_image.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
# Create label below profile pic
self.label02 = QLabel("Please use a square image", self)
self.label02.setFont(QFont("Calibri", 8))
self.label02.setStyleSheet("color: #D9D9D9;") # Set foreground color
self.label02.adjustSize() # Adjust the label size according to text width
self.label02.move((self.window_width - self.label02.width()) // 2, self.profile_image.y() + self.profile_image.height() + 5)
# Enable mouse press event for profile image
self.profile_image.mousePressEvent = self.handle_profile_image_click
#Create textbox01 with Enter Full Name
self.textbox01 = QLineEdit(self)
self.textbox01.setPlaceholderText("Enter Full Name") # Placeholder text
self.textbox01.setFixedSize(250, 30) # Set fixed size for textbox
self.textbox01.setStyleSheet("""
QLineEdit {
background-color: transparent;
color: #D9D9D9; /* Foreground color */
border: 1px solid #D9D9D9; /* Border color */
font: 12pt "Calibri"; /* Font and size */
}
""")
self.textbox01.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.textbox01.move((self.window_width - self.textbox01.width()) // 2, self.profile_image.y() + self.profile_image.height() + 30)
#Create textbox02 with Enter Sanganak ID
self.textbox02 = QLineEdit(self)
self.textbox02.setPlaceholderText("Enter Sanganak ID")
self.textbox02.setFixedSize(250, 30)
self.textbox02.setStyleSheet("""
QLineEdit {
background-color: transparent;
color: #D9D9D9; /* Foreground color */
border: 1px solid #D9D9D9; /* Border color */
font: 12pt "Calibri"; /* Font and size */
}
""")
self.textbox02.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.textbox02.move((self.window_width - self.textbox02.width()) // 2, self.textbox01.y() + self.textbox01.height() + 10)
# Create textbox03 with Enter E-Mail ID
self.textbox03 = QLineEdit(self)
self.textbox03.setPlaceholderText("Enter E-Mail ID")
self.textbox03.setFixedSize(250, 30)
self.textbox03.setStyleSheet("""
QLineEdit {
background-color: transparent;
color: #D9D9D9; /* Foreground color */
border: 1px solid #D9D9D9; /* Border color */
font: 12pt "Calibri"; /* Font and size */
}
""")
self.textbox03.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.textbox03.move((self.window_width - self.textbox03.width()) // 2, self.textbox02.y() + self.textbox02.height() + 10)
# Create textbox04 with Enter Mobile Number
self.textbox04 = QLineEdit(self)
self.textbox04.setPlaceholderText("Enter Mobile Number")
self.textbox04.setFixedSize(250, 30)
self.textbox04.setStyleSheet("""
QLineEdit {
background-color: transparent;
color: #D9D9D9; /* Foreground color */
border: 1px solid #D9D9D9; /* Border color */
font: 12pt "Calibri"; /* Font and size */
}
""")
self.textbox04.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.textbox04.move((self.window_width - self.textbox04.width()) // 2, self.textbox03.y() + self.textbox03.height() + 10)
# Connect returnPressed signal to launch_create_id
self.textbox04.returnPressed.connect(self.launch_create_id)
# Create the button
self.submit_button = QPushButton("&Submit", self)
self.submit_button.setFixedSize(150, 30)
# Set button styles
self.submit_button.setStyleSheet(
"""
QPushButton {font-family: Calibri; font-size: 20px; color: #FFDE59; background-color: #162D2D; border: 1px solid #FFDE59; border-radius: 5px;}
QPushButton:hover {background-color: #405C54; box-shadow: 0px 0px 10px 2px #FFDE59; color: #FFDE59;}
"""
)
# Position the button (center horizontally, 65 pixels from bottom)
self.submit_button.move((self.window_width - self.submit_button.width()) // 2, self.window_height - 65)
# Set cursor change on hover
self.submit_button.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
# Connect the button to the method
self.submit_button.clicked.connect(self.launch_create_id)
# Set the keyboard shortcut (ALT+S)
self.submit_button.setShortcut("Alt+S")
# Use QTimer to delay setting focus to ensure all widgets are initialized
QTimer.singleShot(0, self.set_usersetup_button_focus)
def set_usersetup_button_focus(self):
# Clear focus from textbox01 and set focus to the submit button
self.textbox01.clearFocus()
self.submit_button.setFocus()
class CreateID(QWidget):
def __init__(self, parent: "Setup"):
super().__init__(parent)
self.parent = parent
self.window_width = 500
self.window_height = 450
self.assets_dir = "Assets" # Assets directory
self.temp_image_path = os.path.join(self.assets_dir, "TempProfile.png")
self.profile_image_path = os.path.join(self.assets_dir, "ProfilePic.png")
db_path = os.path.join(self.assets_dir, "Sanganak.db")
# Create the CreateID label
label01 = QLabel("CREATE ID", self)
label01.setFont(QFont("Georgia", 25)) # Set font to Georgia, size 25
label01.setStyleSheet("color: #FFDE59;") # Set foreground color
label01.adjustSize() # Adjust the label size according to text width
label01.move((self.window_width - label01.width()) // 2, 10) # Horizontally center and position vertically 10px down
# Create the horizontal line (QFrame)
line = QFrame(self)
line.setFrameShape(QFrame.Shape.HLine) # Set the frame to be a horizontal line
line.setStyleSheet("background-color: #D9D9D9;") # Set line color
line.setGeometry(0, label01.y() + label01.height() + 1, self.window_width, 1) # Position the line 1px below the label
# Load and position the profile image
self.profile_image = QLabel(self)
if os.path.exists(self.profile_image_path): # Check if ProfilePic.png exists
pixmap = QPixmap(self.profile_image_path)
else:
pixmap = QPixmap(self.temp_image_path)
self.profile_image.setPixmap(pixmap)
self.profile_image.setScaledContents(True) # Scale the image if necessary
self.profile_image.resize(100, 100) # Set label size
self.profile_image.setStyleSheet("border: 1px solid #FFDE59;")
self.profile_image.move((self.window_width - self.profile_image.width()) // 2, label01.y() + label01.height() + 20)
# Query the database to get the first name
first_name = "User" # Default fallback if something goes wrong
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Fetch the FullName from the SetUp table
cursor.execute("SELECT FullName FROM SetUp LIMIT 1")
result = cursor.fetchone()
if result:
full_name = result[0]
# Extract the text before the first space
first_name = full_name.split(" ")[0] if " " in full_name else full_name
conn.close()
except Exception as e:
print(f"Error accessing database: {e}")
# Create label below profile pic
self.label02 = QLabel(f"Hello, {first_name}!", self)
self.label02.setFont(QFont("Calibri", 8))
self.label02.setStyleSheet("color: #FFDE59;") # Set foreground color
self.label02.adjustSize() # Adjust the label size according to text width
self.label02.move((self.window_width - self.label02.width()) // 2, self.profile_image.y() + self.profile_image.height() + 5)
# Create textbox01 with Set User ID
self.textbox01 = QLineEdit(self)
self.textbox01.setPlaceholderText("Set User ID") # Placeholder text
self.textbox01.setFixedSize(250, 30) # Set fixed size for textbox
self.textbox01.setStyleSheet("""
QLineEdit {
background-color: transparent;
color: #D9D9D9; /* Foreground color */
border: 1px solid #D9D9D9; /* Border color */
font: 12pt "Calibri"; /* Font and size */
}
""")
self.textbox01.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.textbox01.move((self.window_width - self.textbox01.width()) // 2, self.profile_image.y() + self.profile_image.height() + 30)
# Create textbox02 with Set Password
self.textbox02 = QLineEdit(self)
self.textbox02.setPlaceholderText("Set Password")
self.textbox02.setFixedSize(250, 30)
self.textbox02.setStyleSheet("""
QLineEdit {
background-color: transparent;
color: #D9D9D9; /* Foreground color */
border: 1px solid #D9D9D9; /* Border color */
font: 12pt "Calibri"; /* Font and size */
}
""")
self.textbox02.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.textbox02.move((self.window_width - self.textbox02.width()) // 2, self.textbox01.y() + self.textbox01.height() + 10)
# Create the button
self.submit_button = QPushButton("&Submit", self)
self.submit_button.setFixedSize(150, 30)
# Set button styles
self.submit_button.setStyleSheet(
"""
QPushButton {font-family: Calibri; font-size: 20px; color: #FFDE59; background-color: #162D2D; border: 1px solid #FFDE59; border-radius: 5px;}
QPushButton:hover {background-color: #405C54; box-shadow: 0px 0px 10px 2px #FFDE59; color: #FFDE59;}
"""
)
# Position the button (center horizontally, 65 pixels from bottom)
self.submit_button.move((self.window_width - self.submit_button.width()) // 2, self.window_height - 65)
# Set cursor change on hover
self.submit_button.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
# Set the keyboard shortcut (ALT+S)
self.submit_button.setShortcut("Alt+S")
# Connect the button click signal to the parent's method
self.submit_button.clicked.connect(self.launch_security)
# Use QTimer to delay setting focus to ensure all widgets are initialized
QTimer.singleShot(0, self.set_createid_button_focus)
def set_createid_button_focus(self):
# Clear focus from textbox01 and set focus to the submit button
self.textbox01.clearFocus()
self.submit_button.setFocus()
1