Change default path in kivy FileChooserIconView module - path

I want to change the default path location (to the desktop) of the FileChooserIconView kivy module. My attempt:
main.py
class LoadDialog(FloatLayout):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
def get_default_path(self):
self.path = os.path.expanduser("~/Desktop") ## Get desktop path.
my.kv
<LoadDialog>:
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserIconView:
id: filechooser
path: root.get_default_path() ## THE PROBLEM. Calling but not working.

I needed to add a return statment to the get_default_path function:
#main.py
class LoadDialog(FloatLayout):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
def get_default_path(self):
self.path = os.path.expanduser("~/Desktop")
return self.path ## I added the return statement
#my.kv
<LoadDialog>:
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserIconView:
id: filechooser
path: root.get_default_path() ## WORKING

Related

How to disable a widget when a button from a different class is pressed?

Im making a Button which disables a TopAppBar menu form my user interface, the problem is that the Button is on one class an the TopAppBar in another, because one belongs to the screen and the other is shared between multiple screens.
code:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivymd.uix.screen import MDScreen
Window.size = (300, 500)
navigation_helper = """
<UserScreen>
name: 'user_screen'
MDBoxLayout:
orientation: "vertical"
pos_hint: {"top": 1}
adaptive_height: True
padding: (0, "80dp" , 0, 0)
FloatLayout:
MDRaisedButton:
id: save_btn_user
text: "Guardar"
md_bg_color: "white"
text_color: "black"
font_size: 16.2
pos_hint: {"left":1, "bottom": 1}
size_hint: (.2,.2)
disabled: True
on_press: root.save_btn_press()
MDRaisedButton:
id: edit_btn_user
text: "Editar"
md_bg_color: "white"
text_color: "black"
font_size: 16.2
pos_hint: {"right":1, "bottom": 1}
size_hint: (.2,.2)
disabled: False
on_press:
root.disable_top_bar()
<MainScreen>:
name: "main_screen"
BoxLayout:
orientation: 'vertical'
MDTopAppBar:
id: title_bar
title: 'Dietas'
md_bg_color: 0.08,0.07,0.45
specific_text_color: 1,1,1
left_action_items: [["menu", lambda x: nav_drawer.set_state('toggle')]]
Widget:
MDNavigationLayout:
ScreenManager:
id: scr
UserScreen:
MDNavigationDrawer:
id: nav_drawer
BoxLayout:
orientation: 'vertical'
spacing: '8dp'
ScrollView:
MDList:
OneLineIconListItem:
text: 'Usuario'
on_press:
scr.current= 'user_screen'
title_bar.title = "Usuario"
nav_drawer.set_state('close')
IconLeftWidgetWithoutTouch:
icon: 'account'
on_press:
scr.current= 'user_screen'
title_bar.title = "Usuario"
nav_drawer.set_state('close')
MainScreen:
"""
class UserScreen(MDScreen):
def disable_top_bar(self):
self.a=MainScreen()
self.a.disable_top_barr()
class MainScreen(MDScreen):
def disable_top_barr(self):
self.ids.title_bar.disabled = True
print("testmsg")
class DemoApp(MDApp):
def build(self):
self.theme_cls.theme_style = ("Dark")
screen = Builder.load_string(navigation_helper)
return screen
DemoApp().run()
As you may have seen, I tried creating two method, one in the TopAppBar class disabling it and printing a test message(that its shown) and the other in the Button class calling that function.
With the following approach
def disable_top_bar(self):
self.a=MainScreen()
self.a.disable_top_barr()
you will see no change in your GUI because each time you call that method, it will create a new instance of MainScreen (which is not related to your GUI) and call the asked method on it.
In order to access that specific instance you can use the method get_running_app of MDApp as follows,
def disable_top_bar(self):
# First access the running app instance.
app = MDApp.get_running_app()
# Access the target screen that you set as the root widget.
main_screen = app.root
# Call the method.
main_screen.disable_top_barr()
As a note avoid using predefined names like a, b, g etc. when working with KivyMD widgets.

When creating a widget with a button, how to specify an ID?

So I already make an expanding list when pressing a button, the problem is that I canĀ“t find I way to add an icon in the right part(as the sample one) of the ListItem
Code:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivymd.uix.screen import MDScreen
from kivymd.uix.list import OneLineRightIconListItem
from kivymd.uix.list.list import IconRightWidget
Window.size = (300, 500)
navigation_helper = """
<DietScreen>:
Screen:
MDBoxLayout:
orientation: 'vertical'
padding: ("5dp", "65dp" , "5dp", "5dp")
pos_hint: {"top": 1}
MDScrollView:
MDList:
id: box
OneLineRightIconListItem:
id: hola
text: 'testitem'
on_press:
IconRightWidget:
icon: "dots-vertical"
MDRaisedButton:
text: "Nueva Dieta"
md_bg_color: "white"
text_color: "black"
font_size: 16.2
pos_hint: {"right": 1, "bottom": 1}
on_press: root.buttonClicked()
Screen:
MDNavigationLayout:
ScreenManager:
id: scr
DietScreen:
"""
class DietScreen(MDScreen):
def buttonClicked(self):
newButt = OneLineRightIconListItem(text='Button')
self.ids.box.add_widget(newButt)
class DemoApp(MDApp):
def build(self):
screen = Builder.load_string(navigation_helper)
return screen
DemoApp().run()
I tried to specify an id to the ListItem created, and in the same method after addind the ListItem, add an IconRightWidget with an icon determined, but it results in an error.
Replace your:
newButt = OneLineRightIconListItem(text='Button')
With:
newButt = OneLineRightIconListItem(IconRightWidget(icon='dots-vertical'), text="Button")

How to Update KivyMD Label in the first screen with on_enter function?

Give below is the kivyMD screen manager program from https://github.com/attreyabhatt/KivyMD-Basics/blob/master/13%20-%20Switching%20Screens/main.py
and I have added it to the official navigation drawer program https://kivymd.readthedocs.io/en/latest/components/navigation-drawer/index.html of KivyMD.
This program works perfectly, but I want to change the text in the MenuScreen with the on_enter function.
I have commented the def on_enter inside class MenuScreen.
If we run it uncommented shows this error:
Traceback (most recent call last):
File "kivy\properties.pyx", line 861, in kivy.properties.ObservableDict.__getattr__
KeyError: 'label_id'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/kivy_prj1/kivy_mainmd/nav_screen.py", line 170, in <module>
sm.add_widget(MenuScreen(name='menu'))
File "C:\Users\Administrator\pyenv\lt_kivyvirt\lib\site-packages\kivy\uix\screenmanager.py", line 997, in add_widget
self.current = screen.name
File "kivy\properties.pyx", line 498, in kivy.properties.Property.__set__
File "kivy\properties.pyx", line 545, in kivy.properties.Property.set
File "kivy\properties.pyx", line 600, in kivy.properties.Property.dispatch
File "kivy\_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1154, in kivy._event.EventObservers._dispatch
File "C:\Users\Administrator\pyenv\lt_kivyvirt\lib\site-packages\kivy\uix\screenmanager.py", line 1062, in on_current
screen.dispatch('on_enter')
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:/Users/Administrator/PycharmProjects/kivy_prj1/kivy_mainmd/nav_screen.py", line 156, in on_enter
self.ids.label_id.text = "Label text Updated"
File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
'label_id' is the id of my label that is to be changed.
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, ListProperty
from kivymd.app import MDApp
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import OneLineIconListItem, MDList
from kivy.uix.screenmanager import ScreenManager, Screen
KV = '''
# Menu item in the DrawerList list.
<ItemDrawer>:
theme_text_color: "Custom"
on_release: self.parent.set_color_item(self)
IconLeftWidget:
id: icon
icon: root.icon
theme_text_color: "Custom"
text_color: root.text_color
<ContentNavigationDrawer>:
orientation: "vertical"
padding: "8dp"
spacing: "8dp"
AnchorLayout:
anchor_x: "left"
size_hint_y: None
height: avatar.height
Image:
id: avatar
size_hint: None, None
size: "56dp", "56dp"
source: "data/logo/kivy-icon-256.png"
MDLabel:
text: "KivyMD library"
font_style: "Button"
size_hint_y: None
height: self.texture_size[1]
MDLabel:
text: "kivydevelopment#gmail.com"
font_style: "Caption"
size_hint_y: None
height: self.texture_size[1]
ScrollView:
DrawerList:
id: md_list
Screen:
ScreenManager:
MenuScreen:
ProfileScreen:
UploadScreen:
NavigationLayout:
ScreenManager:
Screen:
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Navigation Drawer"
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.toggle_nav_drawer()]]
Widget:
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
id: content_drawer
<MenuScreen>:
name: 'menu'
MDRectangleFlatButton:
text: 'Profile'
pos_hint: {'center_x':0.5,'center_y':0.6}
on_press: root.manager.current = 'profile'
MDRectangleFlatButton:
text: 'Upload'
pos_hint: {'center_x':0.5,'center_y':0.5}
on_press: root.manager.current = 'upload'
MDLabel:
id:label_id
text: 'update this at on enter'
pos_hint: {'center_x':0.5,'center_y':0.4}
halign: 'center'
<ProfileScreen>:
name: 'profile'
MDLabel:
text: 'Profile'
halign: 'center'
MDRectangleFlatButton:
text: 'Back'
pos_hint: {'center_x':0.5,'center_y':0.1}
on_press: root.manager.current = 'menu'
<UploadScreen>:
name: 'upload'
MDLabel:
text: 'Upload'
halign: 'center'
MDRectangleFlatButton:
text: 'Back'
pos_hint: {'center_x':0.5,'center_y':0.1}
on_press: root.manager.current = 'menu'
'''
class ContentNavigationDrawer(BoxLayout):
pass
class ItemDrawer(OneLineIconListItem):
icon = StringProperty()
text_color = ListProperty((0, 0, 0, 1))
"""Instead of just giving MDList in kv file give DrawerList for adding the ThemableBehavior with it and created color
changing fn """
class DrawerList(ThemableBehavior, MDList):
def set_color_item(self, instance_item):
"""Called when tap on a menu item."""
# Set the color of the icon and text for the menu item.
for item in self.children:
if item.text_color == self.theme_cls.primary_color:
item.text_color = self.theme_cls.text_color
break
instance_item.text_color = self.theme_cls.primary_color
# print(f"inst:{instance_item.text_color}", f"thm_txt{self.theme_cls.text_color}",
# f"thm_prm{self.theme_cls.primary_color}")
class MenuScreen(Screen):
pass
# def on_enter(self, *args):
# self.ids.label_id.text = "Label text Updated"
class ProfileScreen(Screen):
pass
class UploadScreen(Screen):
pass
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(ProfileScreen(name='profile'))
sm.add_widget(UploadScreen(name='upload'))
class TestNavigationDrawer(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
icons_item = {
"folder": "My files",
"account-multiple": "Shared with me",
"star": "Starred",
"history": "Recent",
"checkbox-marked": "Shared with me",
"upload": "Upload",
}
for icon_name in icons_item.keys():
self.root.ids.content_drawer.ids.md_list.add_widget(
ItemDrawer(icon=icon_name, text=icons_item[icon_name])
)
TestNavigationDrawer().run()
So anybody knows how to change the first screens label or anything from on_enter in KivyMD?
Note: Uncomment the # def on_enter(self, *args): # self.ids.label_id.text = "Label text Updated" this.
It will show the error.
I am having the same problem.
It would seems like the error starts here : sm.add_widget(MenuScreen(name='menu'))
while adding the class MenuScreen to the screen manager the on_enter is evoked.
This would usually solved by adding the Builder at the top, but for KivyMD i don't think it is not possible.
I haven't exactly find an answer, but found a way around(have to run app somehow).
Step 1: Remove these lines:
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(ProfileScreen(name='profile'))
sm.add_widget(UploadScreen(name='upload'))
edit the kv-program like this:
Screen:
ScreenManager:
MenuScreen:
name:"menu"
ProfileScreen:
name:"profile"
UploadScreen:
name:"upload"
You can use self.parent.current = "screen_name" inside any Screen manager class in the python program instead of sm.current = "screen_name.
And if you needed to change screen inside of a layout which is inside of other and so.. instead of using self.parent.parent... you can use MDApp.get_running_app().root.ids.screen_manger.current = "screen_name"
Step2: To avoid on_enter problem for the first screen create a logo screen that show up only at the start of application like this:
Add this in python program
class HomeScreen(Screen):
pass
and edit Kv-program like this:
Screen:
ScreenManager:
HomeScreen:
name:"home"
MenuScreen:
name:"menu"
ProfileScreen:
name:"profile"
UploadScreen:
name:"upload"
and add this to the kv-program
<HomeScreen>:
BoxLayout:
Button:
size:self.size
on_press:
root.manager.current = "menu"
You can add image or animation on the button.

remove_widget in kivy doesn't remove widget

I'm having trouble removing a widget using remove_widget in kivy
I either get TypeError: 'Label' object has no attribute 'getitem' error
or it just doesn't remove the label that was created.
I think I'm not properly referring to the widget made but not sure how to...
Builder.load_string("""
<MenuScreen>:
FloatLayout:
#cols: 2
#rows: 2
size: 800,480
Button:
id: resetmash
text: "Reset"
font_size: 30
size_hint: None, None
background_color: 1,0.88,0.882,1
size: 100,100
pos: 1450,800
on_press: root.resetmash()
Button:
id: btn_0
text: "+"
size_hint: None, None
size: 100,100
pos: 550,700
on_press: root.listmashsteps()
""")
class MenuScreen(Screen):
def resetmash(self):
self.remove_widget(Label())
def listmashsteps(self, *largs):
self.add_widget(Label(text="Step"))
def __init__(self, **kwargs):
super(MenuScreen, self).__init__(**kwargs)
sm = ScreenManager()
menu_screen = MenuScreen(name='menu')
sm.add_widget(menu_screen)
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
self.remove_widget(Label())
This doesn't make sense - you just instantiated that Label, so it isn't already added to the widget, so you can't remove it.
Instead store a reference to the Label when you add it, something like:
self.label = Label(text='Step')
self.add_widget(self.label)
Then later:
self.remove_widget(self.label)

Kivy, passing argument using roulette and carousel

main:
# -*- coding: utf-8 -*-
import kivy
kivy.require('1.8.0')
'''
Check aida.kv for the ui design
'''
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import (ObjectProperty, ListProperty, StringProperty, NumericProperty)
class Controls(BoxLayout):
timer_value = NumericProperty()
def __init__(self, **kwargs):
super(Controls, self).__init__(**kwargs)
self.roulette_time.bind(rolling_value=self.time_changed)
def time_changed(self, instance, value):
self.timer_value = value
class WeatherRoot(BoxLayout):
pass
class AidaApp(App):
pass
if __name__ == '__main__':
AidaApp().run()
kv:
# -*- coding: cp1252 -*-
#:kivy 1.8.0
#:import CyclicRoulette kivy.garden.roulette.CyclicRoulette
WeatherRoot:
<WeatherRoot#BoxLayout>:
carousel: carousel
controls: controls
BoxLayout:
orientation: "vertical"
Carousel:
id: carousel
Controls:
id: controls
<Controls>:
canvas.before:
Color:
rgba: 0.686, 0.635, 0.541, 0.5
Rectangle:
pos: self.pos
size: self.size
roulette_time: rlt_time
BoxLayout:
CyclicRoulette:
cycle: 60
density: 15
zero_indexed: True
selected_value: 5
width: 50
background_color: [0.686, 0.635, 0.541, 1]
id: rlt_time
Label:
size_hint: (1, .8)
text: format(root.timer_value)
font_size: 50
I get an error 'Controls'object has no attribute 'roulette_time
When I set an attribute I get sort of different errors about binding, etc
only works when in kv Controls set to root, but I must have a different class as a root
Please help me to solve this problem, I am stucked
You get the error Controls'object has no attribute 'roulette_time' because nowhere in your Controls class, or anywhere for that matter, do you define a roulette_time variable. Try setting roulette_time = NumericProperty(0) in the Controls class.
class Controls(BoxLayout):
roulette_time = NumericProperty(0)
Not sure why that's not working, because it should. An ObjectProperty should automatically be created to hold the reference to the CyclicRoulette instance.
But - there's an easier way to do this! You're just updating a property on your Controls class with that value anyway, so you can directly bind them. Replace this line:
roulette_time: rlt_time
with:
timer_value: rlt_time.rolling_value
and whenever the CyclicRoulettes rolling_value property changes, your timer_value on Controls will automatically be updated. You don't need to create the time_changed method or bind it to the CyclicRoulette.

Resources