My MDToolbar is covering the uppermost part of my label, I want the text of my label to appear below the toolbar also the pos_hint is not working on this I want to set the position of my text of label also I want to provide padding on left and right side here's my code in .kv file
<MainScreen>:
BoxLayout:
canvas:
Color:
rgb: 0, 0, 0, 0
Rectangle:
pos: root.pos
size: root.size
ScrollView:
Label:
text:
"""
A boy and a girl were playing together. The boy had a collection of marbles. The girl has some
sweets with her. The boy told the girl that he would give her all his marbles in exchange for the
sweets with her. The girl agreed.
The boy kept the most beautiful and the biggest marbles with him and gave her the remaining marbles.
The girl gave him all her sweets as she promised. That night the girl slept peacefully. But the boy
could not sleep as he kept wondering if the girl has hidden some sweets from him the way he had
hidden the best marbles from her.
Moral of the Story :
If you do not give 100 percent in a relationship, you will always kept doubting if the other person
has given her / his hundred percent. This is applicable for any relationship like love, employee –
employer, friendship, family, countries, etc…
"""
font_size: '20sp'
height: self.texture_size[1]
size: self.texture_size
text_size: root.width, None
size_hint_x: 1.0
size_hint_y: None
halign: "auto"
valign: "middle"
ScreenManager:
id: screen_manager
MainScreen:
name: 'main'
NavigationLayout:
ScreenManager:
Screen:
MDToolbar:
title: "Story"
anchor_title: 'left'
pos_hint: {"top": 1}
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
MDNavigationDrawer:
title: 'Story A Day'
id: nav_drawer
swipe_distance: 10
ContentNavigationDrawer:
id: content_drawer
screen_manager: screen_manager
nav_drawer: nav_drawer
The problem is that your 'kv' file has two rules for building your MainScreen, and both are being executed. You should combine the two rules into one, so that you can refer to different parts of the MainScreen to help in positioning. As an example, adding an id to the MDToolbar allows positioning of the ScrollView below it. Like this:
<MainScreen>:
BoxLayout:
canvas:
Color:
rgb: 0, 0, 0, 0
Rectangle:
pos: root.pos
size: root.size
ScreenManager:
id: screen_manager
MainScreen:
name: 'main'
NavigationLayout:
ScreenManager:
Screen:
MDToolbar:
id: toolbar # id for use below
title: "Story"
anchor_title: 'left'
pos_hint: {"top": 1}
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
ScrollView:
# use toolbar id to position the ScrollView
pos_hint: {'top': 1.0 - toolbar.height / self.parent.height}
Label:
text:
"""
A boy and a girl were playing together. The boy had a collection of marbles. The girl has some
sweets with her. The boy told the girl that he would give her all his marbles in exchange for the
sweets with her. The girl agreed.
.
.
.
Your kv file is overly complicated for what it does. I haven't made any simplifications because you may have reasons for the way you are doing it.
Related
I'm new to kivy and I want to access the touch Object without overwriting motion event functions like on_touch_down(self,touch), on_touch_up(), etc..
I want to access touch.pos in order to dynamically draw lines on an Image in a Scatter Class.
Simplified my kv file looks like the following:
<Map#Scatter>:
source: None
do_rotation: False
Image:
id: main_image
source: root.parent.parent.parent.parent.map_path
allow_stretch: True
keep_ratio: True
size_hint_y: None
size_hint_x: None
width: self.parent.width
height: self.parent.width/self.image_ratio
<Zoom>:
id: zoom_class
Map:
id: map_class
<RoomMarkerScreen>:
id: room_marker_screen
AnchorLayout:
rows: 2
AnchorLayout:
anchor_x: 'center'
anchor_y: 'top'
Zoom:
id: zoom_class
AnchorLayout:
anchor_x:'center'
anchor_y: 'bottom'
Button:
text: "Fix/Unfix Map"
size_hint: 0.5, 0.05
on_press:
root.fix_unfix(root.fix)
Knowing that Class Zoom Inherits from FloatLayout, how can I access Touch Object in Zoom?
Thanks in advance!
If I take the touch.pos from the event motion functions, they will be overwritten and I won't be able use the drag and scale features that came with the Scatter Class.
Whenever you write a function that exists in a super class, you should call that that super class method, otherwise, you will be interfering in the functioning of that super class (for example, DragBehavior). So, I suggest trying something like:
class Zoom(FloatLayout):
fixed_map = BooleanProperty(False)
def on_touch_down(self, touch):
print(touch.pos)
return super(Zoom, self).on_touch_down(touch)
And returning True from a on_touch_down() method stops all further dispatching of that touch event, so other widgets do not see that event. See the documentation.
Also, from the same documentation:
on_touch_down(), on_touch_move(), on_touch_up() don’t do any sort of
collisions. If you want to know if the touch is inside your widget,
use collide_point().
Can somebody help me change te color of my MDIconButton.
This is my code.
MDIconButton:
icon: "account"
pos_hint:{"center_x": .95, "center_y": .95}
user_font_size: "35sp"
md_bg_color: app.theme_cls.primary_color
I know it would be a simple problem but i am a starter
According to the documentation, you can use text_color:
theme_text_color: "Custom"
text_color: app.theme_cls.primary_color
I want to understand how to layout a KivyMD screen using a GridLayout with ScrollView, and with a BottomNavigation. My code doesn't work because the GridLayout displays over the BottonNavigation which then can't be seen. What is the correct way to setup this layout? And, generally are there rule-of-thumbs for KivyMD screen layouts? Thanks
<MyScreen>:
name: 'myscreen'
ScrollView:
orientation:'vertical'
MDGridLayout:
cols: 3
adaptive_height: True
padding: dp(4), dp(4)
spacing: dp(4)
etc.
BoxLayout:
orientation:'vertical'
MDBottomNavigation:
panel_color: .2, .2, .2, 1
MDBottomNavigationItem:
name: 'left'
text: 'Left'
icon: 'chevron-left'
MDLabel:
text: 'Left'
halign: 'center'
font_style: 'Icon'
MDBottomNavigationItem:
name: 'right'
text: 'Right'
icon: 'chevron-right'
MDLabel:
text: 'Right'
halign: 'center'
font_style: 'Icon'
etc.
The Layout classes monitor the sizes of their children, and recalculates the layout and redraws if that size changes. So, I believe that if your Image size does not change, then only that Image will be updated.
The kivy Builder maintains a set of rules based on the kv files loaded, so whether those rules come from separate files doesn't really matter. I would recommend structuring your kv file(s) based on things like maintainability, readability, and logical partitioning
I have a screen in kivy which I want to scroll as the text in the label widget is not showing all the text, just like in kindle we can scroll down the stories. Also, my MainScreen is inherited from Screen class. And as the text increases it does not show the last 5 or 6 lines of the story
here's my .kv code of that class
```
<MainScreen>:
BoxLayout:
canvas:
Color:
rgb: 0, 0, 0, 0
Rectangle:
size: self.size
Label:
id: story
font_size: '20sp'
height: self.texture_size[1]
size: self.texture_size
text_size: self.width, self.height
halign: "auto"
valign: "center"
pos_hint: {"center_x": 0.7, "center_y": 0.49}
text:
"""A boy and a girl were playing together. The boy had a collection of marbles. The girl has some
sweets with her. The boy told the girl that he would give her all his marbles in exchange for the
sweets with her. The girl agreed.
The boy kept the most beautiful and the biggest marbles with him and gave her the remaining marbles.
The girl gave him all her sweets as she promised. That night the girl slept peacefully. But the boy
could not sleep as he kept wondering if the girl has hidden some sweets from him the way he had
hidden the best marbles from her.
Moral of the Story :
If you do not give 100 percent in a relationship, you will always kept doubting if the other person
has given her / his hundred percent. This is applicable for any relationship like love, employee –
employer, friendship, family, countries, etc…
"""
```
here's the class MainScreen in .py file
class MainScreen(Screen):
pass
In order to make anything scroll, you need to use a ScrollView widget.
Here's how it can be implemented on the code you posted
ScrollView:
size_hint_y:None
GridLayout:
cols:1
size_hint_y:None
height:self.minimum_height
canvas:
Color:
rgb: 0, 0, 0, 0
Rectangle:
size: self.size
Label:
id: story
font_size: '20sp'
height: self.texture_size[1]
size: self.texture_size
text_size: self.width, self.height
halign: "auto"
valign: "center"
pos_hint: {"center_x": 0.7, "center_y": 0.49}
text:
"""A boy and a girl were playing together. The boy had a collection of marbles. The girl has some
sweets with her. The boy told the girl that he would give her all his marbles in exchange for the
sweets with her. The girl agreed.
The boy kept the most beautiful and the biggest marbles with him and gave her the remaining marbles.
The girl gave him all her sweets as she promised. That night the girl slept peacefully. But the boy
could not sleep as he kept wondering if the girl has hidden some sweets from him the way he had
hidden the best marbles from her.
Moral of the Story :
If you do not give 100 percent in a relationship, you will always kept doubting if the other person
has given her / his hundred percent. This is applicable for any relationship like love, employee –
employer, friendship, family, countries, etc…
"""
```
Here is another answer, but this only has the Label in the ScrollView:
<MainScreen>:
BoxLayout:
pos_hint: {"center_x": 0.7, "center_y": 0.49}
size_hint_y: None
height: 150 # make smaller than Label to force scrolling
canvas:
Color:
rgb: 0, 0, 0, 0
Rectangle:
size: self.size
ScrollView:
do_scroll_x: False
Label:
id: story
font_size: '20sp'
size_hint_y: None
height: self.texture_size[1]
text_size: root.width, None
halign: "auto"
valign: "center"
text:
"""A boy and a girl were playing together. The boy had a collection of marbles. The girl has some
sweets with her. The boy told the girl that he would give her all his marbles in exchange for the
sweets with her. The girl agreed.
The boy kept the most beautiful and the biggest marbles with him and gave her the remaining marbles.
The girl gave him all her sweets as she promised. That night the girl slept peacefully. But the boy
could not sleep as he kept wondering if the girl has hidden some sweets from him the way he had
hidden the best marbles from her.
Moral of the Story :
If you do not give 100 percent in a relationship, you will always kept doubting if the other person
has given her / his hundred percent. This is applicable for any relationship like love, employee –
employer, friendship, family, countries, etc…
"""
Read this important part of the ScrollView documentation.
I am relatively new in Kivy. Having studied quite a bit on my laptop, everything works fine until i started working on my surface pro 4. All my buttons were placed in wrong positions, irrespective of how I specify the position, pos or pos_hint, eg:
the window
above is the window that appears when the following code is run:
<Button>:
color: .8, .9, 0, 1
font_size: 31
size_hint: .4, .3
<FloatLayout>
Button:
text: 'button1'
pos_hint: {'x':0, 'top': 1}
Button:
text: 'button2'
pos_hint: {'right': 1, 'y': 0}
could this be due to issues with OpenGL on surface pro 4 or could I have done something totally dumb?
After going through the documentation, I was able to locate the config.ini file on my c: drive and changed the rotation value from 90 to 0. rotation = 0 and my widgets are placed as expected. Kudos to the Kivy.org team, great documentation.