我目前正在尝试使用Toga创建一个跨平台的Beeware应用程序。我知道如何更新窗口中的内容(我只是清空框中的所有内容并向其中添加新内容)。但是现在我有一个问题,我想添加条目字段,这些字段需要分配给一个变量,这样我就可以获得它们的值(我的第一个窗口的类已经很大了……所以我也不想添加新的属性和方法。所以我的目的是在我的另一个类之外创建一个新类,它显示我的新窗口并关闭旧窗口(或者只是替换我的旧窗口)。
例如:
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()谢谢^^
发布于 2021-08-13 17:58:02
我找到了一个解决方案:
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
https://stackoverflow.com/questions/68699551
复制相似问题