Recursive Clear:
Below is added as to my Main Window to allow for use through out my App.
Some Layouts (at bottom of clear) are setup as instance variables for the main window and they will persist, everything else gets deleted.
def clear_section(self, section):
if section == "all":
self.current_layout = self.base_layout
layout = self.base_layout
else:
self.current_layout = section
layout = section
# Remove all children from current layout
for widget_no in range(0, layout.count()):
if "Layout" in str(layout.itemAt(widget_no)):
self.previous_layout = self.current_layout
self.clear_section(layout.itemAt(widget_no))
else:
layout.itemAt(widget_no).widget().deleteLater()
layout.update()
# Remove any extra layouts
if self.current_layout != self.base_layout:
if self.current_layout != self.title_section:
if self.current_layout != self.content_section:
if self.current_layout != self.side_bar_section:
if self.current_layout != self.buttons_section:
self.current_layout.deleteLater()
self.base_layout.update()
# Check if this was a recursive call and if so reset the layout
if self.previous_layout is not None:
self.current_layout = self.previous_layout
self.previous_layout = None
Display a prompt message in Button Section:
Again added as a Main Window method to allow for use throughout App. Is a basic function that is passed the prompt and prompt type. A Response type will display buttons and all others will display a temporary message that is cleared with a QTimer.
I’ve hard coded the ContentsMargin settings since the messages passed are preplanned but you can adjust to tighten up the display.
def messagebox(self, message, mess_type):
if not self.msg_prompt_shown:
self.clear_section(self.buttons_section)
self.response = None
self.import_action = None
if mess_type == "Response":
layout1 = QHBoxLayout()
else:
layout1 = QHBoxLayout()
layout2 = QGridLayout()
icon = QLabel()
if "Warning" in mess_type:
image = QPixmap("./images/warn1.png").scaled(70, 70)
elif mess_type == "Field Validation Error":
image = QPixmap("./images/info2.png").scaled(70, 70)
elif mess_type == "Error":
image = QPixmap("./images/warn1.png").scaled(70, 70)
elif mess_type == "Response":
image = QPixmap("./images/question.png").scaled(70, 70)
self.response = "No"
else:
image = QPixmap("./images/info2.png").scaled(70, 70)
icon.setPixmap(image)
icon.setFixedSize(70, 70)
icon.setAlignment(Qt.AlignmentFlag.AlignCenter)
msg = QLabel()
msg.setText(message)
msg.setFixedSize(500, 70)
msg.setStyleSheet("font-size: 22px; font-weight: 500")
layout2.addWidget(icon, 0, 0, Qt.AlignmentFlag.AlignRight)
layout2.addWidget(msg, 0, 1, Qt.AlignmentFlag.AlignLeft)
#layout1.addLayout(layout2)
if mess_type == "Response":
button = QPushButton()
button.setObjectName("yesBtn")
button.setFlat(True)
button.setIcon(QIcon("./images/yes_up.png"))
button.setFixedSize(64, 64)
button.setIconSize(QSize(64, 64))
button.pressed.connect(lambda btn=button: show_pressed(btn))
button.released.connect(lambda btn=button: self.show_released(btn))
layout2.addWidget(button, 0, 2, Qt.AlignmentFlag.AlignCenter)
button = QPushButton()
button.setObjectName("noBtn")
button.setFlat(True)
button.setIcon(QIcon("./images/no_up.png"))
button.setFixedSize(64, 64)
button.setIconSize(QSize(64, 64))
button.pressed.connect(lambda btn=button: show_pressed(btn))
button.released.connect(lambda btn=button: self.show_released(btn))
layout2.addWidget(button, 0, 3, Qt.AlignmentFlag.AlignCenter)
if mess_type == "Response":
playsound("prompt.wav", False)
layout1.addLayout(layout2)
if len(message) < 40:
layout1.setContentsMargins(106, 0, 106, 0)
else:
layout1.setContentsMargins(60, 0, 60, 0)
self.buttons_section.addLayout(layout1)
self.msg_prompt_shown = True
else:
if "Warning" in mess_type or mess_type == "Field Validation Error" or mess_type == "Error":
playsound("prompt.wav", False)
layout1.addLayout(layout2)
if len(message) < 35:
layout1.setContentsMargins(0, 0, 60, 0)
else:
layout1.setContentsMargins(0, 0, 74, 0)
self.buttons_section.addLayout(layout1)
self.msg_prompt_shown = True
self.timer = QTimer()
self.timer.setSingleShot(True)
self.timer.setInterval(2300)
self.timer.timeout.connect(self.messagebox_action)
self.timer.start()
def messagebox_action(self):
if self.response == "Yes":
self.import_rtn()
elif self.response == "Save Anyways":
self.continue_with_save()
self.response = None
self.msg_prompt_shown = False
self.import_action = None
self.show_buttons()
To use …
self.messagebox("some text", "Response")
self.messagebox("some warning text", "Warning"
Results:
before prompt …
after prompt is executed …