Search code examples
pythonuser-interfacebeeware

Best way to replace Window in Toga (Beeware)


I'm currently trying to create a cross platform app with Beeware using Toga. I konw how to update the content in a window (I just empty everything in the box and add new content to it). But now I have the problem that I want to add entry fields which need to be assigned to a variable so I can get the value of them (And my class for the first window is already very big... so I don't want to add new properties and methods too it). So my intention was to create a new class besides my other class which displays my new window and closes the old one (or just replaces my old one)

E. g.:

import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW

class FirstWindow(toga.App):

    def startup(self):
        main_box = toga.Box(style=Pack(direction=COLUMN))

        main_box.add(toga.Button('Open window 2', on_press=self.open_new_window))

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()

    def open_new_window(self, widget):
        # This should close the current Window and display the other window
        # return SecodnWindow() doesn't work
        # close() and exit() doesn't work too, cause I can't execute code after this 
        # statements anymore

class SecondWindow(toga.App):

    def startup(self):
        main_box = toga.Box(style=Pack(direction=COLUMN))

        #adding all the stuff to the window

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()
    
    # Other code

def main():
    return FirstWindow()

Thanks ^^


Solution

  • I found a solution:

    import toga
    from toga.style import Pack
    from toga.style.pack import COLUMN, ROW
    
    class MultiWindow(toga.App):
    
        def startup(self):
    
            main_box = toga.Box()
            main_box.add(toga.Button('Open Window', on_press=self.show_second_window))
    
            self.main_window = toga.Window(title=self.formal_name, closeable=True)
            self.main_window.content = main_box
            self.main_window.show()
    
        def show_second_window(self, widget):
            outer_box = toga.Box()
            self.second_window = toga.Window(title='Second window')
            self.windows.add(self.second_window)
            self.second_window.content = outer_box
            self.second_window.show()
            self.main_window.close()
    
    
    def main():
        return MultiWindow()
    

    Sadly it's all in one class but I will try to improve it Note: In this solution it is not possible to have a main window. As soon as you close the main window the app will close. When you just want to have a second window without closing the main window you can still use a main window.

    If I find better solutions I will update this gist: https://gist.github.com/yelluw/0acee8e651a898f5eb46d8d2a577578c