gpt4 book ai didi

python - 更新 Toga、Beeware、Python 中的 MainWindow

转载 作者:行者123 更新时间:2023-12-03 08:16:56 27 4
gpt4 key购买 nike

我正在尝试使用 Beeware 创建一个跨平台应用程序,一开始我显示两个按钮供用户选择他想要进入的 View ,因此一旦单击按钮,主窗口应该更新其内容并显示用户选择的 View 。

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

Start

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

enter image description here

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

这是代码:

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

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

提前致谢。

最佳答案

已修复。

我的理解是,我们必须创建一个可以使用的通用框(self.main_box),以及该框的通用子框(self.view_box),这样我们就可以删除和重置该框的内容一般子项,每次修改内容时,MainWindow都会默认刷新自己,因此每次修改内容时都不需要 self.main_window.content = self.main_box

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

关于python - 更新 Toga、Beeware、Python 中的 MainWindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69096068/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com