python gui toga

 

import toga
from toga.style.pack import COLUMN, Pack 

class Tutorial2App(toga.App): 
    def startup(self):
        brutus_icon = "icons/brutus"
        cricket_icon = "icons/cricket-72.png"
        
        def getContainer(): 
            def button_handler(widget):
                print("button handler")
                for i in range(0, 10):
                    print("hello", i)
                    # yield 1
                print("done", i)
 
            column = ["Hello", "World"]
            data = [("root%s" % i, "value %s" % i) for i in range(1, 100)]
            left_container = toga.Table(headings=column, data=data)

            right_content = toga.Box(style=Pack(direction=COLUMN, padding_top=50))

            for b in range(0, 10):
                right_content.add(
                    toga.Button(
                        "Hello world %s" % b,
                        on_press=button_handler,
                        style=Pack(width=200, padding=20),
                    )
                )

            right_container = toga.ScrollContainer(horizontal=False)

            right_container.content = right_content

            split = toga.SplitContainer()
            # The content of the split container can be specified as a simple list:
            #    split.content = [left_container, right_container]
            # but you can also specify "weight" with each content item, which will
            # set an initial size of the columns to make a "heavy" column wider than
            # a narrower one. In this example, the right container will be twice
            # as wide as the left one.
            split.content = [(left_container, 1), (right_container, 2)]
            return split
        
        def getAction():
            def action0(widget):
                print("action 0")


            def action1(widget):
                print("action 1")


            def action2(widget):
                print("action 2")


            def action3(widget):
                print("action 3")


            def action5(widget):
                print("action 5")


            def action6(widget):
                print("action 6")
            # Create a "Things" menu group to contain some of the commands.
            # No explicit ordering is provided on the group, so it will appear
            # after application-level menus, but *before* the Command group.
            # Items in the Things group are not explicitly ordered either, so they
            # will default to alphabetical ordering within the group.
            things = toga.Group("Things")
            cmd0 = toga.Command(
                action0,
                text="Action 0",
                tooltip="Perform action 0",
                icon=brutus_icon,
                group=things,
            )
            cmd1 = toga.Command(
                action1,
                text="Action 1",
                tooltip="Perform action 1",
                icon=brutus_icon,
                group=things,
            )
            cmd2 = toga.Command(
                action2,
                text="Action 2",
                tooltip="Perform action 2",
                icon=toga.Icon.DEFAULT_ICON,
                group=things,
            )

            # Commands without an explicit group end up in the "Commands" group.
            # The items have an explicit ordering that overrides the default
            # alphabetical ordering
            cmd3 = toga.Command(
                action3,
                text="Action 3",
                tooltip="Perform action 3",
                shortcut=toga.Key.MOD_1 + "k",
                icon=cricket_icon,
                order=3,
            )
            
            def action4(widget):
                print("CALLING Action 4")
                cmd3.enabled = not cmd3.enabled


            cmd4 = toga.Command(
                action4,
                text="Action 4",
                tooltip="Perform action 4",
                icon=brutus_icon,
                order=1,
            )

            # Define a submenu inside the Commands group.
            # The submenu group has an order that places it in the parent menu.
            # The items have an explicit ordering that overrides the default
            # alphabetical ordering.
            sub_menu = toga.Group("Sub Menu", parent=toga.Group.COMMANDS, order=2)
            cmd5 = toga.Command(
                action5,
                text="Action 5",
                tooltip="Perform action 5",
                order=2,
                group=sub_menu,
            )
            cmd6 = toga.Command(
                action6,
                text="Action 6",
                tooltip="Perform action 6",
                order=1,
                group=sub_menu,
            )

            return [cmd1, cmd0, cmd6, cmd4, cmd5, cmd3,cmd2]

        cmd1, cmd0, cmd6, cmd4, cmd5, cmd3,cmd2 = getAction()  
        self.main_window = toga.MainWindow()
        # Command 2 has not been *explicitly* added to the app. Adding it to
        # a toolbar implicitly adds it to the app.
        self.main_window.toolbar.add(cmd1,  cmd2,cmd3, cmd4)
        # The order in which commands are added to the app or the toolbar won't
        # alter anything. Ordering is defined by the command definitions.
        self.commands.add(cmd1, cmd0, cmd6, cmd4, cmd5, cmd3)
        self.main_window.content = getContainer()

        self.main_window.show()


def main():
    return Tutorial2App("Tutorial 2", "org.beeware.toga.tutorial", icon='message.ico')


if __name__ == "__main__":
    main().main_loop()

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: Python Toga是一个用于创建跨平台原生GUI应用程序的Python库。它提供了简单易用的API,可以在不同的操作系统上创建和运行应用程序。通过使用Toga,开发人员可以使用Python语言编写应用程序,并在Windows、macOS和Linux等平台上运行。Toga库提供了丰富的组件和布局选项,使开发人员能够创建具有各种功能和外观的应用程序。如果你想自定义Toga应用程序的关于菜单项,你可以重写about()方法,并在其中调用元数据来获取作者、版本、邮箱等信息,并使用info_dialog()方法来显示关于对话框。\[1\]另外,如果你想为Toga应用程序的边框添加填充,你可以使用不同数量的整数参数来指定填充的大小。例如,如果你提供了4个整数参数,它们将分别用作顶部、右侧、底部和左侧的填充值。\[2\] #### 引用[.reference_title] - *1* [【Python自学笔记】Beeware—toga如何自定义about菜单项](https://blog.csdn.net/xiaoqiangclub/article/details/124556331)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【Python自学笔记】Beeware组件Toga学习笔记](https://blog.csdn.net/xiaoqiangclub/article/details/124377587)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值