首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Toga、Beeware、Python中更新MainWindow

在Toga、Beeware、Python中更新MainWindow
EN

Stack Overflow用户
提问于 2021-09-08 01:45:00
回答 1查看 157关注 0票数 1

我正在尝试用Beeware创建一个跨平台的应用程序,一开始我显示了两个按钮,让用户选择他想要的视图,所以一旦按钮被点击,主窗口应该更新其内容并显示用户选择的视图。

这是应用程序启动时的主窗口:

一旦我点击"First View“,第一个视图的内容就会被添加到开始内容的后面,如下所示:

预期的行为是主窗口删除按钮并只显示文本,第二个视图按钮也应该发生同样的情况。

代码如下:

代码语言:javascript
复制
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW


class exampleApp(toga.App):

    def startup(self):
        """
        Construct and show the Toga application.

        Usually, you would add your application to a main content box.
        We then create a main window (with a name matching the app), and
        show the main window.
        """
        main_box = toga.Box(style=Pack(direction=COLUMN))
    
        ###        
        # Main Screen
        first_view = toga.Button('First View', on_press=self.first_view, style=Pack(padding=2))
        second_view = toga.Button('Second View', on_press=self.second_view, style=Pack(padding=2))

        home_box = toga.Box(style=Pack(direction=ROW, padding=2))
        home_box.add(first_view)
        home_box.add(second_view)

        main_box.add(home_box)
        ###

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

    def first_view(self, widget):
        new_box = toga.Box()
        screen_text = toga.Label('This screen will allow you to see your First View')
        new_box.add(screen_text)
        self.main_window.content = new_box

    def second_view(self, widget):
        new_box = toga.Box()
        screen_text = toga.Label('This screen will allow you to see your Second View')
        new_box.add(screen_text)
        self.main_window.content = new_box

def main():
    return exampleApp()

有人知道如何获得预期的输出吗?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-10 12:26:32

已修复。

我所理解的是,我们必须创建一个通用的盒子,我们可以玩(self.main_box),这个盒子有一个通用的孩子(self.view_box),这样我们就可以删除和重置一般孩子的内容,每次内容被修改时,MainWindow默认会自动刷新,所以我们不需要每次修改内容时都需要self.main_window.content = self.main_box

代码语言:javascript
复制
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW


class exampleApp(toga.App):

    def startup(self):
        """
        Construct and show the Toga application.

        Usually, you would add your application to a main content box.
        We then create a main window (with a name matching the app), and
        show the main window.
        """
        self.main_box = toga.Box(style=Pack(direction=COLUMN))
        self.view_box = toga.Box()
        
        ###
        # Main Screen
        first_view = toga.Button('First View', on_press=self.first_view, style=Pack(padding=2))
        second_view = toga.Button('Second View', on_press=self.second_view, style=Pack(padding=2))

        self.view_box.style = Pack(direction=ROW, padding=2)
        self.view_box.add(first_view)
        self.view_box.add(second_view)
        ###
        
        self.main_box.add(self.view_box)

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

    def first_view(self, sender):
        self.main_box.remove(self.view_box)

        self.view_box = toga.Box()
        self.view_box.style = Pack(direction=ROW, padding=2)
        
        screen_text = toga.Label('This screen will allow you to see your First View')
        self.view_box.add(screen_text)
        
        self.main_box.add(self.view_box)
        self.main_window.show()

    def second_view(self, widget):
        self.main_box.remove(self.view_box)

        self.view_box = toga.Box()
        self.view_box.style = Pack(direction=ROW, padding=2)

        screen_text = toga.Label('This screen will allow you to see your Second View')
        self.view_box.add(screen_text)

        self.main_box.add(self.view_box)
        self.main_window.show()

def main():
    return exampleApp()
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69096068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档