Python Forum
Multiple.ui windows showing at the same time when I only want to show at a time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple.ui windows showing at the same time when I only want to show at a time
#5
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.
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.
Reply


Messages In This Thread
RE: Multiple.ui windows showing at the same time when I only want to show at a time - by deanhystad - Dec-20-2022, 05:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Schedule exit a program at a specific time 4 am every day. chubbychub 3 346 May-17-2024, 03:45 PM
Last Post: chubbychub
  Filer and sort files by modification time in a directory tester_V 5 524 May-02-2024, 05:39 PM
Last Post: tester_V
Question Convert UTC now() to local time to compare to a strptime() Calab 2 361 Apr-29-2024, 07:24 PM
Last Post: deanhystad
  Date Time Series Help...Please spra8560 2 470 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 801 Jan-20-2024, 04:45 AM
Last Post: 1418
  time difference bettwenn logs enkliy 14 1,222 Nov-21-2023, 04:51 PM
Last Post: rob101
Question Need Help with Vehicle Routing Problem with Time Windows (VRPTW) in Python kasper321421312 1 707 Nov-10-2023, 08:19 PM
Last Post: snippsat
  How do I stream and record at the same time with arducam? traderjoe 0 536 Oct-23-2023, 12:01 AM
Last Post: traderjoe
  i tried to install python for the first time today and pretty certain im being remote brianlj 2 645 Oct-03-2023, 11:15 AM
Last Post: snippsat
  [Python 2.7] Why can't I press [ESC] a fourth time? Ashwood 3 750 Aug-27-2023, 02:01 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020