做一个按钮打开一个窗口

2024-06-09 01:44:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编码非常陌生,大约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()

Tags: namefromimportselfboxstylemaintoga
3条回答

不确定这是不是正确的方法,但我建议做一个函数,一个方法,把它放在音乐之家的课程里。。。注意自我。关于和补充自我,关于方法。将“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上运行,但我没有安卓系统。我还没有用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()

然后我通过一个按钮在主窗口中调用它

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

或者,您可以使用asyncio函数之一自动打开它:

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

我想这会解决你的问题

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

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)

你弄明白了吗?我也在努力。到目前为止,我已经试过了:

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

新的导航按钮会在主屏幕上渲染,但按下它不会执行任何操作。我想知道它是否会回到主循环

相关问题 更多 >