我对编程非常陌生,大约有两周的经验。但是,对于一个学校项目,我正在尝试创建一个应用程序。为了让我的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()发布于 2020-09-28 07:36:10
你搞清楚了吗?我也在努力。到目前为止,我已经尝试过了:
# 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确实会在主屏幕上呈现,但按下它不会产生任何影响。我想知道它是否会返回到主循环。
发布于 2020-10-10 04:19:04
我不确定这是否是正确的方式,但我建议将函数、方法放在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)
)或者你可以在你的一个异步函数中自动打开它:
thread.Threading(target=showProgressWindow).start()
await asyncio.sleep(7)发布于 2021-09-08 02:49:47
我想这会解决你的问题。
我试过了,它在同一个应用程序上打开了一个新窗口。关闭主窗口是可选的,因此我删除了该行。
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()https://stackoverflow.com/questions/64062049
复制相似问题