如何从分配给 KivyMD Python 中的 MDLists 的 TextFields 获取数据
Posted
技术标签:
【中文标题】如何从分配给 KivyMD Python 中的 MDLists 的 TextFields 获取数据【英文标题】:How to get data from TextFields assigned to MDLists in KivyMD Python 【发布时间】:2021-10-17 19:50:42 【问题描述】:我想从分配给 MDList 的文本字段中获取用户输入并分配给一个列表(变量)。能不能按照我按照的方法来做。
否则
-
能否在可滚动窗口中添加文本字段?如果是这样,那就容易多了
还有其他方法吗?
代码.kv
screen_helper = """
ScreenManager:
NoS:
Neutral:
<NoS>:
name: 'nos'
MDLabel:
text: 'Get Songs to your Studio'
font_style: 'H3'
halign: 'center'
pos_hint: 'center_x':0.5,'center_y':0.75
MDTextField:
id:nos
pos_hint: 'center_x':0.5,'center_y':0.45
size_hint: (0.7,0.1)
hint_text : 'Number of Songs you want to Get'
helper_text: 'Required'
helper_text_mode: 'on_focus'
icon_right: 'account-music'
mode : 'rectangle'
icon_right_color: app.theme_cls.primary_light
required : True
MDRaisedButton:
text: 'Back'
pos_hint: 'center_x':0.1,'center_y':0.08
font_style: 'Button'
ripple_rad_default : 40
ripple_duration_out : 1
md_bg_color: app.theme_cls.accent_dark
on_press:
root.manager.current = 'settingup'
root.manager.transition.direction = 'right'
MDRaisedButton:
text:"Next"
pos_hint: 'center_x':0.9,'center_y':0.08
font_style: 'Button'
ripple_rad_default : 40
ripple_duration_out : 1
md_bg_color: app.theme_cls.primary_dark
on_press:
root.manager.current = 'neutral'
root.manager.transition.direction = 'left'
app.song_info()
MDProgressBar:
value:60
pos_hint:'center_y' : 0.007
<Neutral>:
name : 'neutral'
MDBoxLayout:
orientation: "vertical"
MDToolbar:
md_bg_color: 0,0,0,.000001
specific_text_color: app.theme_cls.primary_light
title: "Get Songs to your Neutral Album"
left_action_items: [["arrow-left", lambda x: app.back_to_nos()]]
ScrollView:
MDList:
id: neutral
padding : '80dp'
MDBoxLayout:
orientation: "vertical"
adaptive_height: True
height: '69dp'
md_bg_color: .07,.07,.07,1
MDRaisedButton:
text : 'Next'
pos_hint : 'center_x':0.9,'center_y':0.08
font_style: 'Button'
ripple_rad_default : 40
ripple_duration_out : 1
md_bg_color: app.theme_cls.primary_dark
on_press:
root.manager.current = 'home'
root.manager.transition.direction = 'left'
MDRaisedButton:
text: 'Back'
id : reset_nos
pos_hint: 'center_x':0.1,'center_y':0.08
font_style: 'Button'
ripple_rad_default : 40
ripple_duration_out : 1
md_bg_color: app.theme_cls.accent_dark
on_press:
root.manager.current = 'nos'
root.manager.transition.direction = 'right'
"""
TextFields 已使用self.screen.get_screen('neutral').ids.neutral.add_widget(MDTextField())
分配给ScrollView
中的MDList
代码.py
>from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.app import MDApp
from kivy.core.window import Window
from kivymd.uix.textfield import MDTextField
Window.size = (900, 600)
class NoS(Screen):
pass
class Neutral(Screen):
pass
sm = ScreenManager()
sm.add_widget(NoS(name='nos'))
sm.add_widget(Neutral(name='neutral'))
class Mode(MDApp):
def build(self):
screen = Screen()
self.screen = Builder.load_string(screen_helper)
screen.add_widget(self.screen)
self.theme_cls.primary_palette = "Blue"
self.theme_cls.accent_palette = 'Red' # 'Red' ,'Yellow'
self.theme_cls.primary_hue = "A700"
self.theme_cls.theme_style = "Dark"
return screen
def song_info(self):
nos = int(self.screen.get_screen('nos').ids.nos.text) + 1
for i in range(1, nos):
if i == 1:
self.screen.get_screen('neutral').ids.neutral.add_widget(
MDTextField(
hint_text=f"Enter the " + str(i) + "st Song you want to Enjoy when you are in Neutral",
pos_hint='center_x': 0.5, 'center_y': 0.45,
size_hint=(0.7, 0.1), icon_right='account-music',
mode='rectangle', icon_right_color=self.theme_cls.primary_light
)
)
elif i == 2:
self.screen.get_screen('neutral').ids.neutral.add_widget(
MDTextField(hint_text=f"Enter the " + str(i) + "nd Song you want to Enjoy when you are in Neutral",
pos_hint='center_x': 0.5, 'center_y': 0.45,
size_hint=(0.7, 0.1), icon_right='account-music',
mode='rectangle', icon_right_color=self.theme_cls.primary_light
)
)
elif i == 3:
self.screen.get_screen('neutral').ids.neutral.add_widget(
MDTextField(hint_text=f"Enter the " + str(i) + "rd Song you want to Enjoy when you are in Neutral",
pos_hint='center_x': 0.5, 'center_y': 0.45,
size_hint=(0.7, 0.1), icon_right='account-music',
mode='rectangle', icon_right_color=self.theme_cls.primary_light)
)
else:
self.screen.get_screen('neutral').ids.neutral.add_widget(
MDTextField(hint_text=f"Enter the " + str(i) + "th Song you want to Enjoy when you are in Neutral",
pos_hint='center_x': 0.5, 'center_y': 0.45,
size_hint=(0.7, 0.1), icon_right='account-music',
mode='rectangle', icon_right_color=self.theme_cls.primary_light)
)
Mode().run()
【问题讨论】:
【参考方案1】:您可以将此方法添加到您的Mode
类中:
def get_songs(self):
neutral = self.screen.get_screen('neutral').ids.neutral
song_list = []
for w in neutral.walk():
if isinstance(w, MDTextField):
song_list.append(w.text)
print('songs:', song_list)
return song_list
然后只需调用该方法即可获取歌曲列表。
【讨论】:
我找不到完美的方法。这种方法能够成功地做到这一点。非常感谢@John Anderson以上是关于如何从分配给 KivyMD Python 中的 MDLists 的 TextFields 获取数据的主要内容,如果未能解决你的问题,请参考以下文章
Python kivy(kivymd)如何从MD存储和加载数据
KivyMD:如何从 python 中添加的 TextField 获取文本