[英]Kivy Dropdown and how to insert corresponding data below the chosen menu in dropdown
我需要帮助。 Python 对我来说几乎是新的,我最初的培训是在 12 月。 我在纯python中做了一个应用,基于不同的数据(主要是天文数据)。 它适用于我的 android 手机,但演示文稿对用户并不友好。 我看到 Kivy 是使 android 应用程序设计精美的好方法。 我的问题从这里开始...... Kivy 在自动训练中从 0 开始处理起来并不简单......我能够在开始时制作一个下拉菜单 下拉菜单有 4 个选项开发下拉菜单开始。 但在那之后,我想把之前计算的数据放在 selected menu selected menu的下方。 我的问题开始了。 我尝试了很多东西:屏幕管理器,在布局中包含下拉菜单等...我在互联网上阅读了很多示例,但我没有找到将相应计算数据放在所选菜单下方的方法。 我很确定我不是第一个尝试这个的人......这里的 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)
之后让 ScreenManager 在包含下拉按钮的 BoxLayout 中
DropdownDemo.__init__
中self.orientation = 'vertical'
所以 ScreenManager 位于下拉按钮下方
在def callback(self, instance, x):
方法中,根据用户的选择选择 ScreenManager 屏幕
对于重音符号,我必须使用 Notepad++ 将 .kv 文件保存在 Ansi 中(在 Ansi 中编码/转换)
希望我了解您的需求。
问候
在这里你可以看到修改后的源图片:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.