我正在尝试用Beeware创建一个跨平台的应用程序,开始时我会显示两个按钮供用户选择他想要的视图,因此一旦单击按钮,主窗口应该更新其内容并显示用户选择的视图
这是应用程序启动时的主窗口:
单击“第一视图”后,第一视图的内容将添加到开始内容后面,如下所示:
预期的行为是在主窗口中删除按钮并仅显示文本,第二个视图按钮也应该如此
代码如下:
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_框),这个框有一个通用子框(self.view_框),这样我们就可以删除和重置通用子框的内容,并且每次修改内容时,主窗口默认会刷新自己,所以
self.main_window.content = self.main_box
不是每次我们修改内容时都需要相关问题 更多 >
编程相关推荐