【问题标题】:Best way to replace Window in Toga (Beeware)在 Toga (Beeware) 中替换 Window 的最佳方法
【发布时间】:2021-10-12 09:48:38
【问题描述】:

我目前正在尝试使用 Toga 创建一个带有 Beeware 的跨平台应用程序。 我知道如何更新窗口中的内容(我只是清空框中的所有内容并向其中添加新内容)。 但是现在我有一个问题,我想添加需要分配给变量的输入字段,以便我可以获得它们的值(而且我的第一个窗口的类已经非常大了......所以我不想要添加新的属性和方法呢)。所以我的意图是在我的其他类之外创建一个新类,它显示我的新窗口并关闭旧窗口(或只是替换我的旧窗口)

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()

谢谢^^

【问题讨论】:

    标签: python user-interface beeware


    【解决方案1】:

    我找到了解决办法:

    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()
    

    遗憾的是,这一切都在一个班级中,但我会努力改进它 注意:在此解决方案中,不可能有一个主窗口。关闭主窗口后,应用程序就会关闭。当您只想拥有第二个窗口而不关闭主窗口时,您仍然可以使用主窗口。

    如果我找到更好的解决方案,我会更新这个要点: https://gist.github.com/yelluw/0acee8e651a898f5eb46d8d2a577578c

    【讨论】:

      猜你喜欢
      • 2021-11-04
      • 2021-04-12
      • 1970-01-01
      • 2017-08-23
      • 2021-07-17
      • 2020-12-20
      • 1970-01-01
      • 2018-02-07
      • 2011-02-19
      相关资源
      最近更新 更多