Kivy FloatLayout issue: Label not centered per default
Question:
I am currently facing a weird situation as my code used to work but does not work in a different context. I basically have a BoxLayout (MainWindow) which contains a first item (BoxLayout), with a canva drawing an Ellipse. The Ellipse is perfectly centered in the BoxLayout. I also have a second BoxLayout which contains some kind of bottom bar.
Nonetheless, when I add a FloatLayout to the first BlockLayout containing my Ellipse, the Label ("Hello") inside it is not centered at all inside the parent BoxLayout nor in the Ellipse.
The end result:
End result with Hello not fitting
from kivymd.app import App
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
Window.size =(290, 590)
class MainWindow(BoxLayout):
pass
class TestApp(App):
def build(self):
return MainWindow()
TestApp().run()
An my kv file:
<MainWindow>:
orientation: "vertical"
background_color: 0,0,0,1
BoxLayout:
id: box
background_color: 0,0,0,1
canvas:
Color:
rgba: 0,0,255,1
Ellipse:
size: 180, 180
pos: [self.center_x - 180/2, self.center_y - 180/2]
FloatLayout:
Label:
text: "Hello"
BoxLayout:
id: BottomBar
orientation: "horizontal"
size_hint: 1, .2
ToggleButton:
background_color: (0,0,0,1) if self.state == "normal" else (255,0,0,1)
text: "Stats"
group: "Bottom"
state: "down"
on_press:
print(root.ids.box.pos)
ToggleButton:
text: "Press"
background_color: (0,0,0,1) if self.state == "normal" else (255,0,0,1)
group: "Bottom"
I have try to change pos, pos_hint and size of every single element without much success. It feels like the FloatLayout element is not even part of its parent BoxLayout.
I also tried GridLayout instead of BoxLayout which does not make it better.
Answers:
Use RelativeLayout instead of FloatLayout:
RelativeLayout:
Label:
text: "Hello"
I am currently facing a weird situation as my code used to work but does not work in a different context. I basically have a BoxLayout (MainWindow) which contains a first item (BoxLayout), with a canva drawing an Ellipse. The Ellipse is perfectly centered in the BoxLayout. I also have a second BoxLayout which contains some kind of bottom bar.
Nonetheless, when I add a FloatLayout to the first BlockLayout containing my Ellipse, the Label ("Hello") inside it is not centered at all inside the parent BoxLayout nor in the Ellipse.
The end result:
End result with Hello not fitting
from kivymd.app import App
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
Window.size =(290, 590)
class MainWindow(BoxLayout):
pass
class TestApp(App):
def build(self):
return MainWindow()
TestApp().run()
An my kv file:
<MainWindow>:
orientation: "vertical"
background_color: 0,0,0,1
BoxLayout:
id: box
background_color: 0,0,0,1
canvas:
Color:
rgba: 0,0,255,1
Ellipse:
size: 180, 180
pos: [self.center_x - 180/2, self.center_y - 180/2]
FloatLayout:
Label:
text: "Hello"
BoxLayout:
id: BottomBar
orientation: "horizontal"
size_hint: 1, .2
ToggleButton:
background_color: (0,0,0,1) if self.state == "normal" else (255,0,0,1)
text: "Stats"
group: "Bottom"
state: "down"
on_press:
print(root.ids.box.pos)
ToggleButton:
text: "Press"
background_color: (0,0,0,1) if self.state == "normal" else (255,0,0,1)
group: "Bottom"
I have try to change pos, pos_hint and size of every single element without much success. It feels like the FloatLayout element is not even part of its parent BoxLayout.
I also tried GridLayout instead of BoxLayout which does not make it better.
Use RelativeLayout instead of FloatLayout:
RelativeLayout:
Label:
text: "Hello"