Dec-20-2022, 05:14 AM
(This post was last modified: Dec-20-2022, 05:14 AM by deanhystad.)
I don't think you understand how Qt works. It, and most gui toolkits work the same way, is based on an event/action paradigm. You create a bunch of controls, define a bunch of actions, and then bind your actions to events that are raised when the user interacts with the controls. An event manager looks up the action to perform when the event is raised, and executes your action code. In Qt, the events are called "signals", the actions are called "slots", and the event manager is exec() (or exec_())..
Your while loop doesn't make any sense in a gui program. The sequence of code execution is controlled by the user, not the order of statements in your program. Instead of a loop you need to write a function or method that is called when the user closes (or tries to close) one of the windows. This function checks if the window can be closed and decides which window to open next.
Below is a simple example. It creates a bunch of windows that have a button. The button clicked signal (action) is connected to a method (action) that closes the current window and draws the next window. I randomly have the windows refuse when asked to close. In a real program the window would refuse to close if it was an uncompleted form.
Your while loop doesn't make any sense in a gui program. The sequence of code execution is controlled by the user, not the order of statements in your program. Instead of a loop you need to write a function or method that is called when the user closes (or tries to close) one of the windows. This function checks if the window can be closed and decides which window to open next.
Below is a simple example. It creates a bunch of windows that have a button. The button clicked signal (action) is connected to a method (action) that closes the current window and draws the next window. I randomly have the windows refuse when asked to close. In a real program the window would refuse to close if it was an uncompleted form.
from PySide6.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout, QMessageBox import random class Window(QWidget): def __init__(self, id, callback): super().__init__() self.button = QPushButton(text=f'Button {id}', parent=self) self.button.clicked.connect(callback) layout = QVBoxLayout(self) layout.addWidget(self.button) def close(self): if random.choice((False, False, True)): # I cannot be closed at this time. raise RuntimeError("I'm busy.") super().close() class WindowManager: """A thing that decides which window to draw next""" def __init__(self): self.windows = [] self.index = 0 def add(self, window): if not self.windows: window.show() self.windows.append(window) def next(self): """This is where you would put your logic that decides which window to draw next. Here I just display windows sequentially. """ window = self.windows[self.index] try: window.close() self.index += 1 if self.index < len(self.windows): self.windows[self.index].show() except RuntimeError as msg: QMessageBox.information(QMessageBox(window), "Sorry", msg.args[0]) def main(): app = QApplication() manager = WindowManager() for i in range(1, 10): manager.add(Window(i, manager.next)) app.exec() main()I agree with Larz60+ in thinking that a Jupyter notebook might be a great choice for doing something like this. A Jupyter notebook has a kind of sequential execution built in. Changes made to the first page can affect what happens in the second page. Maybe more importantly, the presentation is also sequential in nature, like paging through a really impressive lab notebook.