简体   繁体   中英

Make a button open a Window on Beeware

I'm very new to coding, about 2 weeks in. But, for a school project, I'm trying to create an app. To make my Python run on android, I'm using the Beeware suite. So, what I want to do is, on Android, when I click a button, I want it to open a new window. Like when you're on instagram and you click on someone's profile. Here's the code I've done til now.

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

Did you figure this out? I'm working on it too. I've tried this so far:

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

Then define a function we called out on the "on_press" for the button above:

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

The new nav_button does render on the main screen, but pressing it does nothing. I'm wondering if it just kicks back to the main loop.

Not sure if it's the correct way but I suggest making about function, a Method and put under Class Casa_musica... Mind the self.about and added self, on the Method. Moved "main_box.add(hello)" up.

Please try and tell me how it goes. :)

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

This is what I did, it works on Mac and Windows but not I have no luck with Android. I haven't tested it with iOS though.

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

Then I just called it in MainWindow through a button...

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

Or you can auto open it in one of your asyncio functions:

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

I think this will solve your question.

I tried it, and it opens a new window on the same app. Closing the main window is optional so I deleted that line.

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

found on : Best way to replace Window in Toga (Beeware)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM