Kivy Dropdown以及如何在下拉菜单中的所选菜单下方插入相应的数据

Kivy Dropdown and how to insert corresponding data below the chosen menu in dropdown

我需要帮助。 Python 对我来说几乎是新的,我最初的培训是在 12 月。我根据不同的数据(主要是天文数据)在纯 python 中做了一个应用程序。它与我的 android phone 配合得很好,但演示文稿并不是真正的用户友好。我看到 Kivy 是制作 android 设计精美的应用程序的好方法。从这里开始我的问题......Kivy 从 0 开始并不容易处理,在自动训练中...... 我能够制作一个下拉菜单 Dropdown menu at start with 4 choices Developed dropdown menuto begin. But after that, I would like to put data previously calculated just below the chosen menu chosen menu。我的问题开始了。 我尝试了很多东西:screenmanager,在布局中包含下拉菜单等......我在互联网上阅读了很多示例,但我没有找到将相应计算数据放在所选菜单下方的方法。 我很确定我不是第一个尝试这个的人...... 这里的 python 代码:

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

'''
Code of How to use drop-down list with.kv file
'''

# Program to Show how to create a switch
# import kivy module
import kivy

# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App

# this restrict the kivy version i.e
# below this kivy version you cannot
# use the app or software
kivy.require('1.11.1')

# drop-down menu is a list of items that
# appear whenever a piece of text or a
# button is clicked.
# To use drop down you must have ti import it
from kivy.uix.dropdown import DropDown

# module consists the floatlayout
# to work with FloatLayout first
# you have to import it
from kivy.uix.floatlayout import FloatLayout

# The Button is a Label with associated actions that
# are triggered when the button is pressed (
# or released after a click / touch).
from kivy.uix.button import Button

from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout

class CustomDropDown(DropDown):
    pass

class DropdownDemo(BoxLayout):
    #The code of the application itself.

    def __init__(self, **kwargs):
        #The button at the opening of the window is created here,
        #not in kv

        super(DropdownDemo, self).__init__(**kwargs)
        self.dropdown = CustomDropDown()

        # Creating a self widget bouton
        self.mainbutton = Button(text='Cliquer pour choisir!', font_size=18, color = [0.2,0.65,0.8,1], pos_hint={"top":1},
                                 size_hint_x=1, size_hint_y=0.08)

        # Added button to FloatLayout so inherits this class
        self.add_widget(self.mainbutton)

        # Adding actions
        # If click
        self.mainbutton.bind(on_release=self.dropdown.open)

        # root.select on_select called
        self.dropdown.bind(on_select=lambda \
                instance, x: setattr(self.mainbutton, 'text', x))
        self.dropdown.bind(on_select=self.callback)

    def callback(self, instance, x):
        #x is self.mainbutton.text refreshed
        print("The chosen mode is: {0}".format(x))

# Declare both screens
class Demarrage(Screen):
    pass

class Soleil(Screen):
    u = 3.141592
    pass

class Ephemerides(Screen):
    pass

# Create the screen manager
sm = ScreenManager()

class dropdownApp(App):
    '''The build function returns root,
    here root = DropdownDemo ().
    root can only be called in the kv file.
    '''

    def build(self):
        #sm = ScreenManager()
        sm.add_widget(Demarrage(name='init'))
        sm.add_widget(Soleil(name='soleil'))
        sm.add_widget(Ephemerides(name='ephemerides'))
        sm.current = 'init'
        u = "essai " + str(3.1415)
        return DropdownDemo()
        #return sm

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

这里是kv代码:

#:kivy 1.11.1

#:set color_font   (0.2, 0.65, 0.8, 1)  # bleu kivy 5rgb 0 164 ou 154? 206
#:set color_button (0.345, 0.345, 0.345, 1)  # gris menu kivy rgb 88 88 88
#:set color_button_pressed (0.4, 0.4, 0.4, 1)  # gris foncé


<CustomDropDown>:
    size: self.size
    Button:
        text: 'Ephémérides générales'
        # background_color: color_button if self.state == 'down' else color_button_pressed
        # color: 0.2,0.65,0.8,1
        # https://github.com/kivy/kivy/wiki/Styling-a-Spinner-and-SpinnerOption-in-KV
        size_hint_y: None
        height: 44
        on_release: root.select('>> Ephémérides Générales' )
    Button:
        text: 'Soleil'
        size_hint_y: None
        height: 44
        on_release: root.select('>> Soleil')
    Button:
        text: 'Lune'
        size_hint_y: None
        height: 44
        on_release: root.select('>> Lune')
    Button:
        text: 'Temps significatifs - TU'
        size_hint_y: None
        height: 44
        on_release: root.select('>> Temps significatifs - TU')

<Demarrage>:

<Soleil>:
    BoxLayout:
        Button:
            text: 'Soleil'
            size_hint_y: None
            height: 44


<Ephemerides>:
    BoxLayout:

<Demarrage>:
    BoxLayout:

非常感谢您的帮助。

我对您的源代码进行了以下更改:

  • self.add_widget(sm)self.add_widget(self.mainbutton) 之后在包含下拉按钮的 BoxLayout 中设置 ScreenManager

  • self.orientation = 'vertical' in DropdownDemo.__init__ 所以屏幕管理器在下拉按钮下面

  • def callback(self, instance, x):方法中,根据用户的选择选择ScreenManager屏幕

  • 对于口音,我不得不使用 Notepad++(在 Ansi 中编码/转换)将 .kv 文件保存在 Ansi 中

希望我了解您的需求。

此致

这里可以看到修改源的图片: