I want to hide the progress bar at below code as soon as screen appears. In order to do that I defined a hide_widget function and added the code to "on_pre_enter" section. However it doesn’t work. What am I missing here ?
By the way if you can introduce a shorter way to hide a widget in kivy, it is also very welcome.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
Builder.load_string("""
<MyLayout>
BoxLayout:
orientation: "vertical"
size: root.width, root.height
spacing: 20
padding: 50
Button:
text: "Test"
ProgressBar:
id: pgb_excel_read
min: 0
max: 100
value: 0
size_hint: ( 1, 0.1)
""")
def hide_widget(wid, dohide=True):
if hasattr(wid, 'saved_attrs'):
if not dohide:
wid.height, wid.size_hint_y, wid.opacity, wid.disabled = wid.saved_attrs
del wid.saved_attrs
elif dohide:
wid.saved_attrs = wid.height, wid.size_hint_y, wid.opacity, wid.disabled
wid.height, wid.size_hint_y, wid.opacity, wid.disabled = 0, None, 0, True
class MyLayout(Widget):
def on_pre_enter(self, *args):
hide_widget(self.ids.pgb_excel_read, True)
class MyApp(App):
def build(self):
return MyLayout()
if __name__ == '__main__':
MyApp().run()
>Solution :
on_pre_enter function is only avaliable for Screen classes. But you try to use it in your custom Widget. So there is a nothing to trigger this function. We can trigger it by __init__:
class MyLayout(Widget):
def __init__(self):
super(MyLayout, self).__init__()
hide_widget(self.ids.pgb_excel_read, True)
For hide any widget we just need to change 2 things:
- Disabled : True
- Opacity : 0
Short way to do that, lets stop triggering any function and just change variables: (Also Remove hide_widget function)
class MyLayout(Widget):
def __init__(self):
super(MyLayout, self).__init__()
# hide_widget(self.ids.pgb_excel_read, True)
self.ids.pgb_excel_read.opacity = 0
self.ids.pgb_excel_read.disabled = True