KivyMD - Cannot Update TextField's text when used in DialogBox - python

I have used KivyMD to develop a Screen which displays the Parameter values (In a DialogBox) of specific Item (which I listed them as OnelinelistItem). I also want to make provision for the user to change the parameter values from the DialogBox. But apparently I cant update the parameter settings from the DialogBox. The DialogBox contains the Textfield. Can anyone help me to figure out the issue, by going through the code, and let me know, where I am doing it wrong?
TIA! :)
testingsetpointpage.py
'''
from kivy.config import Config
Config.set('kivy', 'keyboard_mode', 'systemanddock')
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen
from kivymd.uix.dialog import MDDialog
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivymd.uix.button import MDFlatButton
from kivy.properties import StringProperty
Window.size = (1024, 600)
The Builder is shown here:
KV = """
#:kivy 1.11.0
#:import MDDropdownMenu kivymd.uix.menu.MDDropdownMenu
#:import MDRaisedButton kivymd.uix.button.MDRaisedButton
ScreenManager:
MainScreen:
<MainScreen>:
name: 'mainscreen'
NavigationLayout:
ScreenManager:
Screen:
name: 'homemain'
BoxLayout:
orientation:"vertical"
halign:"center"
#DOWN TAB
MDBottomNavigation:
MDBottomNavigationItem:
name: 'setpoint'
text: 'Setpoints'
icon: 'network'
BoxLayout:
orientation: "vertical"
MDToolbar:
title: 'Setpoints'
pos_hint: {'center_x':0.5,'center_y':0.95}
right_action_items: [["wifi", lambda x: app.navigation_draw()]]
left_action_items: [["menu", lambda x: nav_drawer.toggle_nav_drawer()]]
elevation: 10
BoxLayout:
orientation:"vertical"
padding: 5
spacing: 5
MDLabel:
text: " Functions: "
halign:"left"
theme_text_color: "Custom"
text_color: 0, 0, 1, 1
size_hint_y: 0.15
canvas.before:
Color:
rgba: (211/255.0,211/255.0,211/255.0,1)
Rectangle:
size: self.size
pos: self.pos
SetpointContent:
#MAKE THE PARAMETER LIST
<SetpointContent>:
BoxLayout:
orientation: "vertical"
padding: 5
spacing: 5
ScrollView:
MDList:
OneLineListItem:
text: "51P: Phase Time Overcurrent"
on_press: root.show_51Pdata()
<Content51P>
alarmpick51p: alp51p
alarmdelay51p: ald51p
trippick51p: trp51p
invcurve51p: inp51p
orientation: "vertical"
size_hint_y: None
height: "200dp"
GridLayout:
rows: 4
cols: 2
spacing: 10
MDLabel:
text: "Alarm Pickup: "
halign: "center"
theme_text_color: "Custom"
text_color: 0, 0, 1, 1
size_hint_y: 0.15
size_hint_x: 0.4
width:100
MDTextFieldRect:
id: alp51p
text: root.text1
multiline: False
MDLabel:
text: "Alarm Delay: "
halign: "center"
theme_text_color: "Custom"
text_color: 0, 0, 1, 1
size_hint_y: 0.15
size_hint_x: 0.4
width:100
MDTextFieldRect:
id: ald51p
text: str(app.Aldelay51P)
multiline: False
MDLabel:
text: "Trip Pickup: "
halign: "center"
theme_text_color: "Custom"
text_color: 0, 0, 1, 1
size_hint_y: 0.15
size_hint_x: 0.4
width:100
MDTextFieldRect:
id: trp51p
text: str(app.Trpick51P)
multiline: False
MDLabel:
text: "Inverse Curve: "
halign: "center"
theme_text_color: "Custom"
text_color: 0, 0, 1, 1
size_hint_y: 0.15
size_hint_x: 0.4
width:100
MDTextFieldRect:
id: inp51p
text: str(app.InverseCurve)
multiline: False
#####
"""
and the Classes are defined here:
class Content51P(BoxLayout):
app=MDApp.get_running_app()
text1 = StringProperty("1")
alarmpick51p = ObjectProperty()
alarmdelay51p = ObjectProperty()
trippick51p = ObjectProperty()
invcurve51p = ObjectProperty()
class MainScreen(Screen):
class SetpointContent(Screen):
def show_51Pdata(self):
self.dialog = MDDialog(title="51P Parameters:",
type="custom",
content_cls=Content51P(),
buttons=[MDFlatButton(text='Close', on_release=self.close_dialog),
MDFlatButton(text='Update', on_release=self.update51P)]
)
self.dialog.auto_dismiss = False
self.dialog.open()
def update51P(self, obj):
duc = Content51P()
app = MDApp.get_running_app()
duc.text1 = duc.ids.alp51p.text
print(duc.text1)
app.Alpick51P = float(duc.text1)
print(app.Alpick51P)
def close_dialog(self, obj):
self.dialog.auto_dismiss = True
self.dialog.dismiss()
class MainApp(MDApp):
Alpick51P = ObjectProperty("5")
Aldelay51P = ObjectProperty("5")
Trpick51P = ObjectProperty("5")
InverseCurve = ObjectProperty("Very Inverse")
def build(self):
self.theme_cls.primary_palette = "Blue"
screen = Builder.load_string(KV)
return screen
def navigation_draw(self):
print("Navigation")
def on_start(self):
pass
if __name__ == '__main__':
MainApp().run()
'''
The outlook looks something like this. I want to update the four parameters as the user clicks on the Update button and be able to view the value, the next time I open the DialogBx.

In the kv rule, under <Content51P> for MDTextFieldRect textinputs add:
on_text: app.Alpick51P = self.text
on_text: app.Aldelay51P = self.text
on_text: app.Trpick51P = self.text
on_text: app.InverseCurve = self.text
The on_text method should go to respective MDTextFieldRect.
Update the update51P() function should be like below now:
def update51P(self, obj):
app = MDApp.get_running_app()
print(app.Alpick51P)
print(app.Aldelay51P)
print(app.Trpick51P)
print(app.InverseCurve)
This would now print the updated inputs from textinput fields.

Related

Showing tasks only after new program launch. kivy

Good evening. I'm learning kivy and trying to make something like "microsoft to do". But I have a problem. Now I'm doing an adding task function. The main idea of it:
When I click "add task"(button with plus icon) button, my screen changes on "AddingNewTaskScreen".
In AddingNewTaskScreen class I created a function ""addTask that takes text from TextInput and add it in text file. Then it changes this screen on "MainMenuScreen"
In MainMenuScreen class(it is first screen), in constructor i call a "taskRead" function that takes text from text file and write it in array. Then it adds elements of arrays in MDList and after it, it adds this MDList to ScrollView.
And it works, but it takes shows me tasks after new programm launch. For example: I add the task and the "MainMenuScreen" doesn't show me anything. But when I restart my programm, everything are in ScrollView. If you have any ideas how it can be fixed, please tell me, I wold really apreciate it. Thank you for advance.
main.py
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivymd.app import MDApp
from kivymd.uix.list import OneLineListItem, MDList
from kivy.properties import ObjectProperty
from kivy.uix.scrollview import ScrollView
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class MainMenuScreen(Screen):
listBox = ObjectProperty()
theme_changer = ObjectProperty()
search_task = ObjectProperty()
all_tasks = ObjectProperty()
important_tasks = ObjectProperty()
create_list = ObjectProperty()
completed_tasks = ObjectProperty()
list_name = ObjectProperty()
sort = ObjectProperty()
rename = ObjectProperty()
add_button = ObjectProperty()
scroll = ScrollView()
list_view = MDList()
def __init__(self, **kw):
super().__init__(**kw)
self.taskRead()
def taskRead(self):
sv = open('tasks.txt', 'r', encoding='utf-8')
arr = sv.read().split('\n')
sv.close()
for i in range(len(arr)):
self.list_view.add_widget(OneLineListItem(text=arr[i], on_press=self.scr))
self.add_widget(MDList())
self.scroll.add_widget(self.list_view)
self.listBox.add_widget(self.scroll)
def scr(self, value):
self.manager.transition = FadeTransition()
self.manager.current = 'goodbye_screen'
class ToDoListApp(MDApp):
def __init__(self, **kw):
super().__init__(**kw)
def build(self):
self.theme_cls.theme_style = "Dark"
Window.size = (1300, 700)
sm = ScreenManager()
sm.add_widget(MainMenuScreen(name='main_menu_screen'))
sm.add_widget(ImportantOrCompletedTaskCheckScreen(name='goodbye_screen'))
sm.add_widget(AddingNewTaskScreen(name='adding_newTask_screen'))
return sm
class AddingNewTaskScreen(Screen):
taskToTxt = ObjectProperty()
addTaskButton = ObjectProperty()
def __init__(self, **kw):
super().__init__(**kw)
def addTask(self):
task_text = str(self.taskToTxt.text)
sv = open('tasks.txt', 'a', encoding='utf-8')
sv.writelines(str(task_text) + '\n')
sv.close()
ToDoListApp.get_running_app().root.current = "main_menu_screen"
# Class for another problem
class ImportantOrCompletedTaskCheckScreen(Screen):
pass
if __name__ == '__main__':
ToDoListApp().run()
todolist.kv
<MainMenuScreen#Screen>:
listBox:listBox
rename:rename
search_task:search_task
theme_changer:theme_changer
all_tasks:all_tasks
important_tasks:important_tasks
create_list:create_list
completed_tasks:completed_tasks
list_name:list_name
sort:sort
#add_button:add_button
orientation: 'vertical'
# right lateral panel
BoxLayout:
BoxLayout:
size_hint: '0.3', '0.8'
pos_hint: {'y': 0.17}
orientation: 'vertical'
GridLayout:
cols: 1
rows: 9
MDRoundFlatButton:
id: theme_changer
size_hint: 0.5, 0.3
text: 'Light theme'
line_color: 1,1,1,1
elevation_normal: 0
on_press: root.change_theme()
BoxLayout:
size_hint: 1, 0.5
MDRoundFlatButton:
size_hint: 1, 0.7
text: 'All tasks'
line_color: 1,1,1,1
id: all_tasks
BoxLayout:
size_hint: 1, 0.08
MDRoundFlatButton:
size_hint: 1, 0.7
text: 'Important'
line_color: 1,1,1,1
id: important_tasks
BoxLayout:
size_hint: 1, 0.08
MDRoundFlatButton:
size_hint: 1, 0.7
text: '+ Create \nlist'
line_color: 1,1,1,1
id: create_list
BoxLayout:
size_hint: 1, 0.08
MDRoundFlatButton:
size_hint: 1, 0.7
text: 'Completed \ntasks'
line_color: 1,1,1,1
id: completed_tasks
# top panel
BoxLayout:
orientation: 'vertical'
AnchorLayout:
anchor_y: 'top'
size_hint: 1, 0.05
GridLayout:
size_hint: 1, 0.05
cols: 4
rows: 1
Label:
text: 'Name'
size_hint: 0.5, 0.05
id: list_name
MDTextField:
id:search_task
halign: 'center'
valign: 'center'
hint_text: 'Search'
size_hint: 1, None
line_color_normal: 0.15,0.15,0.15
MDRectangleFlatButton:
id: rename
text: 'Rename'
line_color: 1,1,1,1
MDRectangleFlatButton:
text: 'Sort'
line_color: 1,1,1,1
id: sort
# Center
BoxLayout:
orientation: 'vertical'
size_hint: 0.1, 0.1
MDFloatingActionButton:
icon: "plus"
md_bg_color: 'black'
on_press: root.manager.current = 'adding_newTask_screen'
BoxLayout:
orientation: 'vertical'
size_hint: 1,1
BoxLayout:
ScrollView:
line_color_normal: 1,1,0,1
id: listBox
# Screen for another problem
<ImportantOrCompletedTaskCheckScreen#Screen>:
name: 'goodbye_screen'
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
BoxLayout:
orientation: 'vertical'
size_hint: 0.5, 0.25
GridLayout:
cols: 2
rows: 1
size_hint: 0.3, 0.25
pos_hint: {'x': 0.35}
MDSwitch:
size_hint: 0.3, 0.3
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
line_color: 1, 1, 1, 1
Label:
text: 'Important'
BoxLayout:
size_hint: 0, 0.5
GridLayout:
cols: 2
rows: 1
size_hint: 0.3, 0.25
pos_hint: {'x': 0.35}
MDSwitch:
text: 'Important'
size_hint: 0.3, 0.3
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
line_color: 1, 1, 1, 1
Label:
text: 'Completed'
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
MDRoundFlatButton:
text: 'Continue'
size_hint: 1, 0.5
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
line_color: 1, 1, 1, 1
on_press: root.manager.current='main_menu_screen'
<AddingNewTaskScreen#Screen>:
taskToTxt:taskToTxt
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
BoxLayout:
orientation: 'vertical'
size_hint: 0.5, 0.25
MDTextField:
id: taskToTxt
hint_text: 'Name of new task'
color: 'white'
halign: 'center'
valign: 'center'
size_hint: 1, None
multiline: False
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
MDRoundFlatButton:
text: 'Add task'
id: addTaskButton
on_press: root.addTask()
size_hint: 1, 0.5
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
line_color: 1, 1, 1, 1
In addTask you should get access to main screen
main_screen = ToDoListApp.get_running_app().root.get_screen('main_menu_screen')
and then you can add task to list
main_screen.list_view.add_widget(OneLineListItem(text=task_text, on_press=main_screen.scr))
def addTask(self):
task_text = str(self.taskToTxt.text)
sv = open('tasks.txt', 'a', encoding='utf-8')
sv.writelines(str(task_text) + '\n')
sv.close()
main_screen = ToDoListApp.get_running_app().root.get_screen('main_menu_screen')
main_screen.list_view.add_widget(OneLineListItem(text=task_text, on_press=main_screen.scr))
ToDoListApp.get_running_app().root.current = "main_menu_screen"
BTW:
ScreenManager is a parent for every Screen so you could write shorter with self.parent.
Or you could use self.manager
main_screen = self.parent.get_screen('main_menu_screen')
#main_screen = self.manager.get_screen('main_menu_screen')
# ... code ...
self.parent.current = "main_menu_screen"
#self.manager.current = "main_menu_screen"
In MainMenuScreen you could also create function to add single task
class MainMenuScreen(Screen):
def add_task(self, text):
self.list_view.add_widget(OneLineListItem(text=text, on_press=self.scr))
and then you could write it simpler in addTask
main_screen = self.parent.get_screen('main_menu_screen')
main_screen.add_task(task_text)
and in taskRead you could reduce for-loop
for text in arr:
self.add_task(text)

MDSelectionList properties cannot be modified

I am using an MDSelectionList inside a tab with an MDNavigationRail to switch between screens, however, when I try to customize the MDSelectionList, none of the visual changes apply. I thought that maybe it is a problem with the widget itself but then I separated it and the property changes worked. How can I make the changes apply to the selection list? (I am using kivymd 0.104.2.dev0 from the master branch)
My Code:
from kivy.config import Config
Config.set('graphics', 'width', '850')
Config.set('graphics', 'height', '530')
Config.set('graphics', 'minimum_width', '850')
Config.set('graphics', 'minimum_height', '530')
from kivy.lang import Builder
from kivymd.uix.card import MDCard
from kivymd.uix.tab import MDTabsBase
from kivymd.app import MDApp
class SettingsTab(MDCard, MDTabsBase):
pass
class Example(MDApp):
def __init__(self, **kwargs):
super(Example, self).__init__(**kwargs)
self.kv = Builder.load_string('''
#:kivy 2.0.0
<SettingsTab>:
orientation: "vertical"
size_hint: .95, .95
pos_hint: {"center_x": .5, "center_y": .5}
border_radius: 5
radius: [5]
elevation: 20
BoxLayout:
MDNavigationRail:
color_active: app.theme_cls.primary_color
MDNavigationRailItem:
icon: "list-status"
on_release:
screens.current = "downloads_screen"
MDNavigationRailItem:
icon: "cog"
on_release:
screens.current = "settings"
MDNavigationRailItem:
icon: "information"
on_release:
screens.current = "activity_log"
ScreenManager:
id: screens
Screen:
name: "downloads_screen"
Screen:
name: "activity_log"
Screen:
name: "settings"
MDTabs:
SettingsTab:
title: "DOWNLOAD"
MDSelectionList:
overlay_color: 1, 0, 0, .5
icon_bg_color: 0, 0, 0, 0
icon_check_color: 0, 0, 0, 0
OneLineIconListItem:
text: "Just me!"
IconLeftWidget:
icon: "circle-outline"
pos_hint: {"center_x": .5, "center_y": .5}
OneLineIconListItem:
text: "Just me!"
IconLeftWidget:
icon: "circle-outline"
OneLineIconListItem:
text: "Just me!"
IconLeftWidget:
icon: "circle-outline"
OneLineIconListItem:
text: "Just me!"
IconLeftWidget:
icon: "circle-outline"
SettingsTab:
title: "COLOR"
SettingsTab:
title: "INFO"''')
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Blue"
self.theme_cls.accent_palette = "Teal"
return self.kv
if __name__ == '__main__':
Example().run()
Try something like this.
from kivy.lang import Builder
from kivymd.uix.card import MDCard
from kivymd.uix.tab import MDTabsBase
from kivymd.app import MDApp
kv = ("""
<Check#ILeftBodyTouch+MDCheckbox>:
group: 'group'
size_hint: None, None
size: dp(48), dp(48)
<SettingsTab>:
orientation: "vertical"
size_hint: .95, .95
pos_hint: {"center_x": .5, "center_y": .5}
border_radius: 5
radius: [5,]
elevation: 20
BoxLayout:
MDNavigationRail:
color_active: app.theme_cls.primary_color
MDNavigationRailItem:
icon: "cog"
on_release:
screens.current = "settings"
ScreenManager:
id: screens
Screen:
name: "settings"
MDTabs:
SettingsTab:
title: "DOWNLOAD"
MDList:
spacing: 10
OneLineAvatarIconListItem:
text: "Just me!"
Check:
OneLineAvatarIconListItem:
text: "Just me!"
Check:
OneLineAvatarIconListItem:
text: "Just me!"
Check:
""")
class SettingsTab(MDCard, MDTabsBase):
pass
class Example(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.kv = Builder.load_string(kv)
def build(self):
return self.kv
if __name__ == '__main__':
Example().run()

Reference a input box id on kivy screen

I think this problem is easy to solve, but i dont have experience enough to do this.
So, I want to reference the input text (MDTextField on kv file) for the variable "name" that is inside the press() function, so that I can write inside the popup.
I'm learning kivymd for a college project, so, discount the errors and unnecessary informations :/
Here is my kv file
Screen:
id: screen1
NavigationLayout:
id: navigation
ScreenManager:
id: screen_manager
Screen:
id: fact
name: 'fact'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Menu"
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
Widget:
MDLabel:
text: "Fact Checker"
font_size: 56
font_name: "MontserratBold"
pos_hint: {'center_x': 0.75, 'center_y': 0.7}
theme_text_color: "Custom"
text_color: (53/255, 183/255, 1, 1)
MDTextField:
id: input_box
hint_text: "Type here"
font_size: 18
pos_hint: {'center_x': 0.5, 'center_y': 0.45}
size_hint_x: None
width: 500
multiline: True
helper_text: "Type the title of the news"
helper_text_mode: "on_focus"
theme_text_color: "Custom"
text_color: (53/255, 183/255, 1, 1)
line_color_normal: 1, 1, 1, 1
MDRectangleFlatButton:
text: "Check"
pos_hint: {'center_x': 0.5, 'center_y': 0.25}
theme_text_color: "Custom"
text_color: (1, 1, 1, 1)
on_press: app.press()
MDLabel:
markup: True
text: "<Developed by: [color=#cfd0d1]Caio Barreto and Gabriel Queiroz[/color]>"
font_name: "Montserrat"
theme_text_color: "Custom"
text_color: (53/255, 183/255, 1, 0.5)
pos_hint: {'center_x': 0.74, 'center_y': 0.1}
Screen:
name: 'discord'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Menu"
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
Widget:
MDLabel:
text: 'teste1'
Screen:
name: 'add'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Menu"
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
Widget:
MDLabel:
text: 'teste2'
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
ScrollView:
DrawerList:
id: md_list
MDList:
OneLineIconListItem:
text: "Menu"
on_press: screen_manager.current = "fact"
IconLeftWidget:
icon: "home"
OneLineIconListItem:
text: "Discord"
on_press: screen_manager.current = "discord"
IconLeftWidget:
icon: "discord"
OneLineIconListItem:
text: "Add more news"
on_press: screen_manager.current = "add"
IconLeftWidget:
icon: "circle-edit-outline"
Here is my python code:
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivymd.uix.dialog import MDDialog
from kivymd.uix.textfield import MDTextField
from kivymd.uix.button import MDRectangleFlatButton, MDFlatButton
from kivymd.uix.navigationdrawer import NavigationLayout
from kivymd.uix.list import MDList
from kivymd.uix.boxlayout import BoxLayout
from kivymd.theming import ThemableBehavior
from kivy.core.text import LabelBase
from kivy.uix.widget import Widget
from helper import username_input
from kivymd.uix.screen import Screen
LabelBase.register(name="MontserratBold", fn_regular="font/Montserrat-Bold.ttf")
LabelBase.register(name="Montserrat", fn_regular="font/Montserrat-Medium.ttf")
class FactCheckerApp(MDApp):
class ContentNavigationDrawer(BoxLayout):
pass
class DrawerList(ThemableBehavior, MDList):
pass
def build(self):
self.file = Builder.load_file('projeto.kv')
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "LightBlue"
print(self.file.Screen.ids.)
return self.file
def press(self):
name = self.screen1.navigation.screen_manager.fact.input_box.text
close_button = MDFlatButton(text='Close', on_release=self.close_dialog)
self.dialog = MDDialog(title='Answer', text=name, size_hint=(0.5, 1), buttons=[close_button])
self.dialog.open()
def close_dialog(self, obj):
self.dialog.dismiss()
if __name__ == '__main__':
FactCheckerApp().run()
To access the MDTextField, you can just reference it through its id, which is defined in the widget that is the root of the kv rule where the id is defined (in this case, in the Screen). Try changing:
name = self.screen1.navigation.screen_manager.fact.input_box.text
to:
name = self.root.ids.input_box.text
in the press() method.

Python : How add a datepicker in .kv file

Can anyone tell me how to add a date picker or kivy calendar on date TextBox?
I have two file test.py and test.kv file.
test.py
import kivy
import sqlite3 as lite
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.label import Label
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (400, 125)
class Testing(Screen):
pass
class Testing(App):
def build(self):
self.root = Builder.load_file('test.kv')
return self.root
if __name__ == '__main__':
Testing().run()
test.kv
Testing:
BoxLayout:
orientation: "vertical"
padding : 20, 20
BoxLayout:
orientation: "horizontal"
padding: 10, 10
spacing: 10, 10
size_hint_x: .55
Label:
text: "Date"
text_size: self.size
valign: 'middle'
size_hint_x: .2
TextInput:
size_hint_x: .3
BoxLayout:
orientation: "horizontal"
padding : 10, 0
spacing: 10, 10
size_hint: .5, .7
pos_hint: {'x': .25, 'y':.25}
Button:
text: 'Ok'
on_release:
root.dismiss()
Button:
text: 'Cancel'
on_release: root.dismiss()
.kv file
#:import MDTextField kivymd.uix.textfield.MDTextField
MDTextField:
id: set_date
hint_text: 'Start Date'
write_tab: False
on_focus: root.setDate()
.py file
from kivymd.uix.picker import MDDatePicker
def setDate(self,dobj):
self.ids.set_date.text = str(dobj)
pass
def fromDate(self):
self.foc = self.ids.set_date.focus
if(self.foc==True):
MDDatePicker(self.setFDate).open()
else:
print("not")

How should I set a global variable in kivy Python?

My application is simple, the ScreenTwo is showing a form 'Parametreur' with various options to be entered.
What I'm trying to do is to set a 'save' button at the end of this form that will register into a list called 'resultat' all the options furfilled so far leaving '0' in the empty inputs.
The global variable 'resultat' would look like this ['blabla','15/06/2018','31/12/1999','6'].
File.py
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.lang import Builder
Builder.load_file("HadrianRunningApp.kv")
class ScreenMenu(Screen):
pass
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
class Parametreur(FloatLayout):
def build(self):
pass
def submit_input(self):
# Get the student name from the TextInputs
label_name = self.label_text_input.text
resultat.insert(0,label_name)
def submit_input2(self):
date_debut_name = self.date_debut_input.text
resultat.insert(1,date_debut_name)
def submit_input3(self):
date_fin_name = self.date_fin_input.text
resultat.insert(2,date_fin_name)
def submit_input4(self):
var_duree_estimation = self.duree_estimation_input.text
resultat.insert(3,var_duree_estimation)
def save_input(self):
print(resultat)
class Manager(ScreenManager):
screen_menu = ObjectProperty(None)
screen_one = ObjectProperty(None)
screen_two = ObjectProperty(None)
class ScreensApp(App):
def build(self):
m = Manager(transition=WipeTransition())
return m
if __name__=='__main__':
ScreensApp().run()
The Kivy file .kv:
#:kivy 1.9.0
<ScreenMenu>:
BoxLayout:
orientation:'vertical'
size: root.size
padding: "20dp"
Button:
text: "go to Screen 1"
on_press: root.manager.current = 'screen1'
Button:
text: "go to Screen 2"
on_press: root.manager.current = 'Parametreur'
<ScreenOne>:
BoxLayout:
spacing: 20
padding: 20
Button:
text: "go to Screen 2"
on_press: root.manager.current = 'Parametreur'
Button:
text: "go to Menu"
on_press: root.manager.current = 'screen0'
<ScreenTwo>:
Parametreur
<Parametreur>:
label_text_input: label_text
date_debut_input: date_debut
date_fin_input: date_fin
duree_estimation_input: duree_estimation
resultat: resultat
RelativeLayout:
orientation: 'vertical'
GridLayout:
size_hint: (1., 0.11)
pos_hint: {'right': 1, 'center_y': 0.91}
padding: 6
spacing: "4dp"
cols:2
rows:1
Button:
text: "go to Screen 1"
on_press: root.manager.current = 'screen1'
Button:
text: "go to Menu"
on_press: root.manager.current = 'screen0'
GridLayout:
size_hint: (1., 0.8)
pos_hint: {'right': 1, 'center_y': 0.45}
padding: "7dp"
spacing: 5
cols:2
rows:7
Button:
text: "Liste Actif"
Label:
text: " "
BoxLayout:
Label:
text: "Date du début"
BoxLayout:
orientation: "vertical"
Button:
text: "Submit"
on_press: root.submit_input2()
TextInput:
id: date_debut
text: 'dd/mm/YYYY'
BoxLayout:
Label:
text: "Date de fin"
BoxLayout:
orientation: "vertical"
Button:
text: "Submit"
on_press: root.submit_input3()
TextInput:
id: date_fin
text: 'dd/mm/YYYY'
Label:
text: "Pourcentage de séparation \n de la base (validation/test)"
Button:
text: "Open to Close"
Button:
text: "Close to Close"
Button:
text: "les 3 (6 en tout) frontières des VSs"
BoxLayout:
Label:
text: "Durée pour la réestimation \n des modèles (en jours)"
BoxLayout:
orientation: "vertical"
Button:
text: "Submit"
on_press: root.submit_input4()
TextInput:
id: duree_estimation
text: 'dd/mm/YYYY'
Label:
text: "variable selection NMF/Entropy"
Label:
text: "Kernel/damiers/buntcher/\n neurone/XGBoost/Gradient boosting"
# We create the widgets
BoxLayout:
spacing: "0dp"
Label:
id: label_text
text: "0"
font_size: "30dp"
BoxLayout:
orientation: "vertical"
spacing: "3dp"
Button:
text: "Add"
on_release: label_text.text = str(int(label_text.text) + 1)
Button:
text: "Subtract"
on_release: label_text.text = str(int(label_text.text) - 1)
Button:
text: "Submit"
size_hint_x: None
on_press: root.submit_input()
BoxLayout:
padding: "2dp"
spacing: "2dp"
size_hint: (1., 0.50)
pos_hint: {'right': 1, 'center_y': 0.09}
Button:
id: resultat
text:"Save"
size_hint_x: None
on_press: root.save_input()
<Manager>:
id: screen_manager
screen_menu: screen_menu
screen_one: screen_one
screen_two: screen_two
ScreenMenu:
id: screen_menu
name: 'screen0'
manager: screen_manager
ScreenOne:
id: screen_one
name: 'screen1'
manager: screen_manager
ScreenTwo:
id: screen_two
name: 'Parametreur'
manager: screen_manager
Where and How should I set my global variable 'resultat' (which is a list)?
I've solved my problem but I still don't understand how does it work. Setting a variable is Kivy doesn't need to set it in the Python code. But how do Kivy and Python know about its type? Right now, it works as a list without me defining it such as (I intended it to be a list anyway).
How could I set a variable as tuple for example?

Categories