在python中为移动应用程序创建主菜单

时间:2017-09-20 03:35:01

标签: python python-3.x kivy kivy-language

我无法弄清楚如何开始显示主菜单的代码。我想要这个主菜单显示5个不同的选项,当点击它们时,它会打开该页面/选项。我也安装了Kivy,总体目标是制作移动应用程序。我的python体验很低,所以我实际上没有为这个目标编写任何代码,但我有一个完整的布局,我希望应用程序看起来像。

更新:

这是我到目前为止所拥有的。

mainMenu = {}
mainMenu ['1'] = 'Option 1'
mainMenu ['2'] = 'Option 2'
mainMenu ['3'] = 'Option 3'
mainMenu ['4'] = 'Option 4'
mainMenu ['5'] = 'Option 5'

print(mainMenu)
while True:
    selection = input()
    if selection == '1':
        print('Page 1')
    elif selection == '2':
        print('Page 2')
    elif selection == '3':
        print('Page 3')
    elif selection == '4':
        print('Page 4')
    elif selection == '5':
        print('Page 5')
        break

1 个答案:

答案 0 :(得分:1)

请参阅以下示例,并根据您的要求进行修改。我建议阅读大量文档Kivy Dropdown list

实施例

main.py

from kivy.app import App
from kivy.uix.dropdown import DropDown
from kivy.uix.floatlayout import FloatLayout
from kivy.config import Config


class SubMenu(DropDown):
    pass


class MainMenu(FloatLayout):

    def display_selected_submenu(self, instance, x):
        print("Page " + x)


class TestApp(App):
    title = "Kivy Drop-Down List Demo"
    Config.set("graphics", "width", "800")
    Config.set("graphics", "height", "480")

    def build(self):
        return MainMenu()


if __name__ == '__main__':
    TestApp().run()

test.kv

#:kivy 1.10.0
#:import Factory kivy.factory.Factory

<CustomButton@Button>:
    size_hint_y: None
    height: 40
    font_size: 18


<SubMenu>:
    on_select: app.root.display_selected_submenu(self, args[1])

    CustomButton:
        id: button1
        text: 'Open'
        txt: "1"
        on_release: root.select(self.txt)

    CustomButton:
        id: button2
        text: 'Save'
        txt: "2"
        on_release: root.select(self.txt)

    CustomButton:
        id: button3
        text: 'Exit'
        txt: "3"
        on_release: root.select(self.txt)

<MainMenu>:
    canvas.before:
        Color:
            rgba: 0.5, 0.5, 0.5, 0.5
        Rectangle:
            pos: 0,0
            size: self.width, self.height

    Button:
        id: mainbutton
        text: "File Menu"
        font_size: 20
        size_hint: None, None
        size: 150, 50
        top: root.top
        on_release: Factory.SubMenu().open(self)

输出

Exit Selected & Page 3 printed on console