首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让按钮在Beeware上打开一个窗口

让按钮在Beeware上打开一个窗口
EN

Stack Overflow用户
提问于 2020-09-25 18:07:04
回答 3查看 519关注 0票数 2

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

代码语言:javascript
复制
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()
EN

回答 3

Stack Overflow用户

发布于 2020-09-28 07:36:10

你搞清楚了吗?我也在努力。到目前为止,我已经尝试过了:

代码语言:javascript
复制
# 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“上调用的函数:

代码语言:javascript
复制
    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确实会在主屏幕上呈现,但按下它不会产生任何影响。我想知道它是否会返回到主循环。

票数 0
EN

Stack Overflow用户

发布于 2020-10-10 04:19:04

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

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

代码语言:javascript
复制
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测试过它。

代码语言:javascript
复制
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中通过一个按钮调用它。

代码语言:javascript
复制
    btnMain = toga.Button(
        'START',
        on_press=self.showProgressWindow,
        style=Pack(padding=5)
    )

或者你可以在你的一个异步函数中自动打开它:

代码语言:javascript
复制
   thread.Threading(target=showProgressWindow).start()
   await asyncio.sleep(7)
票数 0
EN

Stack Overflow用户

发布于 2021-09-08 02:49:47

我想这会解决你的问题。

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

代码语言:javascript
复制
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)

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

https://stackoverflow.com/questions/64062049

复制
相关文章

相似问题

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