标签
使用Label组件可以创建一个显示文本的标签。以下是创建一个Label组件的代码
import toga
def build(app):
label = toga.Label('Hello, world!', style=Pack(padding=50))
return toga.MainWindow(title=app.name, size=(640, 480)).with_content(
toga.Box(children=[label], style=Pack(direction=COLUMN))
)
if __name__ == '__main__':
app = toga.App('My Label App')
app.startup = build
app.main_loop()
按钮
可以使用Button组件创建一个按钮,以便用户可以与应用程序交互。以下是创建一个Button组件的代码:
import to
def button_handler(widget):
print('Button pressed')
def build(app):
button = toga.Button('Click me!', on_press=button_handler)
return toga.MainWindow(title=app.name, size=(640, 480)).with_content(
toga.Box(children=[button], style=Pack(direction=COLUMN, padding=50))
)
if __name__ == '__main__':
app = toga.App('My Button App')
app.startup = build
app.main_loop()