【问题标题】:Make a button open a Window on Beeware在 Beeware 上制作一个按钮打开一个窗口
【发布时间】:2021-01-11 16:00:58
【问题描述】:

我对编码非常陌生,大约 2 周后。但是,对于一个学校项目,我正在尝试创建一个应用程序。为了让我的 Python 在 android 上运行,我使用了 Beeware 套件。所以,我想做的是,在 Android 上,当我点击一个按钮时,我希望它打开一个新窗口。就像你在 Instagram 上点击某人的个人资料一样。这是我到目前为止完成的代码。

import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW


def about(button, formal_name='a'):
    main_box = toga.Box(style=Pack(direction=COLUMN, background_color='#7ed6c6'))
    hello = toga.Label('Hello there!')
    main_window = toga.MainWindow(title=formal_name)
    main_window.content = main_box
    main_window.show()
    main_box.add(hello)

class Casa_musica(toga.App):

    def startup(self):
        main_box = toga.Box(style=Pack(direction=COLUMN, background_color='#7ed6c6', padding=20))

        button = toga.Button(
            'Click me', on_press=about)
        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()
        main_box.add(button)


def main():
    return Casa_musica()

【问题讨论】:

    标签: python android beeware


    【解决方案1】:

    你发现了吗?我也在努力。到目前为止我已经尝试过了:

    # add a button to the main window
            nav_button = toga.Button(
                'Open second window.',
                on_press=self.nav_window_two,
                style=Pack(padding=5)
            )
    
        # ...
            main_box.add(nav_button)
    

    然后为上面的按钮定义一个我们在“on_press”上调用的函数:

        def nav_window_two(self, widget):
            window_two_box = toga.Box(style=Pack(direction=COLUMN))
            window_two_hello_label = toga.Label(
                'Wow you made it to the second window!',
                style=Pack(padding=(0, 5))
            )
            window_two_box.add(window_two_hello_label)
    
            self.window_two = toga.Window()
            self.window_two.content = window_two_box
            self.window_two.show()
    

    新的 nav_button 确实会在主屏幕上呈现,但按下它什么也不做。我想知道它是否只是回到主循环。

    【讨论】:

    • 好吧,从那以后我就过渡到了 Kivy。这个问题在 Kivy 上更容易解决。它更强大,更易于使用,因为有更多可用的文档。
    【解决方案2】:

    不确定这是否是正确的方法,但我建议制作关于函数、方法并放在 Class Casa_musica 下...注意 self.about 并在方法上添加 self。上移了“main_box.add(hello)”。

    请试着告诉我进展如何。 :)

    class Casa_musica(toga.App):
    
        def startup(self):
            main_box = toga.Box(style=Pack(direction=COLUMN, background_color='#7ed6c6', padding=20))
    
            button = toga.Button(
                'Click me', on_press=self.about)
            self.main_window = toga.MainWindow(title=self.formal_name)
            self.main_window.content = main_box
            self.main_window.show()
            main_box.add(button)
    
        def about(self):
            main_box = toga.Box(style=Pack(direction=COLUMN, background_color='#7ed6c6'))
            hello = toga.Label('Hello there!')
            main_box.add(hello)
            self.main_window = toga.MainWindow(title=formal_name)
            self.main_window.content = main_box
            self.main_window.show()
            
    

    这就是我所做的,它适用于 Mac 和 Windows,但我在 Android 上没有运气。不过我还没有在 iOS 上测试过。

    def showProgressWindow(self):
        main_box2 = toga.Box(style=Pack(direction=COLUMN, padding=5))
    
        self.lblStatus = toga.Label('Here you will see the progress of the process')
        self.tiProgress = toga.MultilineTextInput(style=Pack(flex=1))
        content_box = toga.Box(style=Pack(direction=ROW, padding=5))
        content_box.add(self.tiProgress)
        button_box = toga.Box(style=Pack(direction=ROW, padding=5))
        btnClose = toga.Button(
            'Close this window',
            on_press=self.main_window.show(),
            style=Pack(padding=5)
    
        )
        button_box.add(btnClose)
    
    
        main_box2.add(self.lblStatus)
        main_box2.add(button_box)
        main_box2.add(content_box)
    
    
    
        self.progressWindow = toga.Window(title='Progress')
        self.progressWindow.content = main_box2
        self.progressWindow.show()
    

    然后我只是通过一个按钮在MainWindow中调用它...

        btnMain = toga.Button(
            'START',
            on_press=self.showProgressWindow,
            style=Pack(padding=5)
        )
    

    或者您可以在您的 asyncio 函数之一中自动打开它:

       thread.Threading(target=showProgressWindow).start()
       await asyncio.sleep(7)
    

    【讨论】:

    • @silvalogmc,这个解决方案有效吗?我以前一直在使用 Kivy,但它不像在 PC 和 mac 中编写 python 那样简单。这就是我转向beeware的原因。 Kivy 对我来说有点令人困惑......希望你能在 Kivy 中使用它。 :)
    【解决方案3】:

    我想这会解决你的问题。

    我试过了,它会在同一个应用程序上打开一个新窗口。关闭主窗口是可选的,所以我删除了该行。

    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()
    
    def main():
        return MultiWindow()
    

    发现于: Best way to replace Window in Toga (Beeware)

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-06-17
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      相关资源
      最近更新 更多