'Dynamically setting attributes to MDTextField in KivyMD
I have code below which dynamically creates a bunch of text fields in KivyMD. However, one attribute I need to set, on_text
, will not accept my settings through python (no error thrown, just a blank field on debug), but will accept them through KV.
My questions are: Why is it not accepting my on_text update, and how can I work around it programmatically while maintaining python/KV separation?
Here's the python code:
from kivy.clock import mainthread
from kivymd.app import MDApp
from kivymd.icon_definitions import md_icons
from kivymd.uix.textfield import MDTextField
from kivy.uix.screenmanager import Screen, ScreenManager, NoTransition
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivymd.uix.gridlayout import GridLayout
from kivy.lang import Builder
Builder.load_file('./main.kv')
class HomeScreen(Screen):
def build(self):
return HomeScreen()
class PRApp(MDApp):
def build(self):
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "Gray"
self.sm = ScreenManager(transition=NoTransition())
self.sm.add_widget(HomeScreen(name="home_screen"))
return self.sm
def add_rm_textboxes(self): # sourcery skip: use-fstring-for-concatenation
for i in range(1,21):
textbox = MDTextField(hint_text=str(i)+" RM",mode="fill",helper_text="Enter your TESTED "+str(i)+" RM",helper_text_mode="on_focus")
textbox.on_text = print(textbox.text)
textbox.name = "rm"+str(i)
self.sm.current_screen.ids.pr_grid.add_widget(textbox)
def wait(self):
pass
PRApp().run()
And here's my KV code:
<HomeScreen>:
id: home_screen
BoxLayout:
orientation: 'vertical'
MDBottomNavigation:
id: bottom_nav
MDBottomNavigationItem:
name: 'home_screen'
text: "Home"
icon: 'home'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "PR!"
anchor_title: 'center'
MDLabel:
text: "This is the home screen"
halign: 'center'
MDBottomNavigationItem:
name: 'routine_screen'
text: "Routines"
icon: 'notebook'
on_tab_press: app.wait()
MDLabel:
text: "Routines"
halign: 'center'
MDBottomNavigationItem:
name: 'profile_screen'
text: "Profile"
icon: 'account-circle-outline'
on_tab_press: app.add_rm_textboxes()
MDLabel:
text: "Profile"
halign: 'center'
MDGridLayout:
id: pr_grid
cols: 2
Solution 1:[1]
Whenever you bind a callback to an event or a property that callback is supposed to be a function / method (name) not what it returns. That's why you got None
when you did textbox.bind(text = print("test"))
, you could've done it like, textbox.bind(text = lambda *args : print("test"))
. Also note that a property callback is different from an event callback in kivy.
Here is a modified version of that portion,
def add_rm_textboxes(self): # sourcery skip: use-fstring-for-concatenation
for i in range(1,21):
textbox = MDTextField(hint_text=str(i)+" RM",mode="fill",helper_text="Enter your TESTED "+str(i)+" RM",helper_text_mode="on_focus")
textbox.bind(text = self.callback_method)
# textbox.bind(text = lambda *args : print("test"))
textbox.name = "rm"+str(i)
self.sm.current_screen.ids.pr_grid.add_widget(textbox)
def callback_method(self, txtbx, txt):
print(f"{txtbx.text = }, {txt = }, {txtbx.name = }")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ApuCoder |