I have my simple python code:
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.config import Config
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
Builder.load_file('Matrixkivy.kv')
class MyLayout(Widget):
pass
class MatrixCalc(App):
def build(self):
return MyLayout()
if __name__ == '__main__':
MatrixCalc().run()
And simple .kv code:
<MyLayout>
GridLayout:
size_hint: root.width, root.height
Button:
text: "Button1"
Button:
text: "Button2"
I want to get a GridLayout which is of size of the entire app and two buttons, which both take half of it. However, app displays all things that the app contain in the bottom left corner. How to fix that?
CodePudding user response:
The main problem is that you are using the base Widget
class as a container. You can do that, but the Layout
classes already have the code to handle things like size_hint
and pos_hint
. One fix for your code is to replace:
class MyLayout(Widget):
pass
with:
class MyLayout(FloatLayout):
pass
You should also remove the line:
size_hint: root.width, root.height
Size_hint
values should be numbers typically in the range 0-1
. And add a line that defines either rows
or cols
for the GridLayout
, so your kv
could look like:
<MyLayout>
GridLayout:
cols: 1
Button:
text: "Button1"
Button:
text: "Button2"
Since you are using GridLayout
as a child of MyLayout
, you can just make MyLayout
extend GridLayout
, and then you can eliminate the GridLayout
line from your kv
. With this approach, your MyLayout
class would look like:
class MyLayout(GridLayout):
pass
and the corresponding kv
:
<MyLayout>
cols: 1
Button:
text: "Button1"
Button:
text: "Button2"