Kivy Two Flashcards

1
Q

In your KV file, set the layout to horizontal
Put a ‘Button A’ to the left
Next add four vertical buttons, They should all be spaced 10pixels
Finally place your last button

A

BoxLayoutExample:

<BoxLayoutExample>:
Button:
text: 'A'
size_hint: 'A'
pos_hint: {'center_y': .5}

BoxLayout:
orientation: 'vertical'
spacing: '10dp'
Button:
text: 'B1'
text: 'B2'
text: 'B3'
text: 'B4'

Button:
text: 'C'
size_hint: 1, 1
pos_hint: 1, 1
</BoxLayoutExample>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Import and use something to make a layout where you can put boxes anywhere but start from the very center of the screen.

How would you put your button to the top left? of the screen?

A

from kivy.uix.anchorlayout import AnchorLayout

class AnchorLayoutExample(AnchorLayout):
pass

KV file

AnchorLayoutExample: <- this makes it the main interface

<AnchorLayoutExample>:
# right, left, center
anchor_x: 'right'
# bottom, top center
anchor_y
Button:
text: 'A'
size_hint: .1, .1

anchor_x: 'left'
anchor_y: 'top'
Button:
text: 'A'
size_hint: .1, .1
</AnchorLayoutExample>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Create two boxes side by side at the top center of the screen

A

AnchorLayoutExample:

<AnchorLayoutExample>:
anchor_x: 'center'
anchor_y: 'top'
BoxLayout:
size_hint: .2, .2
Button:
text: 'A'
Button:
text: 'B'

We specify the BoxLayout's size into relation of how much of the AnchorLayout it takes up, if we didn't do this, it would take up everything and the size_hints of the buttons themselves would place them however they would in a standard BoxLayout.
</AnchorLayoutExample>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Organize content with colons and arrows.
Create 6 buttons that will be in 3 columns wide and 2 rows down
Change the first button to be half the size wide, but with a normal sized height (REMBMER, this will be tricky since this will have to apply to both buttons in the same column)
Turn the second button into a Box layout of three buttons vertically

A

from kivy.uix.gridlayout import GridLayout

class GridLayoutExample(GridLayout):
pass

In KV file

GridLayoutExample:

<GridLayoutExample>:
"""Or you don't have to add the gridlayoutclass and can just do this: <GridLayoutExample@GridLayout>:"""
# Need one of two properties: rows, cols
cols: 3
Button:
size_hint: .5, 1
text: 'a'
BoxLayout:
Button:
text'a1'
Button:
text'b1'
Button:
text'c1'
Button:
text: 'c'
Button:
text: 'd'
size_hint: .5, 1
Button:
text: 'e'
Button:
text: 'f'
</GridLayoutExample>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Stack your buttons from left to right using a different method from kivi
If you use 20 percent for all of your buttons and put 6 in your code, how will they come out?

Now do this make buttons with this method using a construtor

A

from kivy.uix.stacklayout import StackLayout

class StackLayoutExample(StackLayout):
pass

from KV file

StackLayoutExample:

<StackLayoutExample>:
Button:
text: "A"
size_hint: .2, .2
Button:
text: "B"
size_hint: .2, .2
Button:
text: "B"
size_hint: .2, .2

If you have 6 buttons you will have 5 stacked from left to right and one below them on the left.

class StackLayoutExample(StackLayout):
def \_\_init\_\_(self, **kwargs)
def super().\_\_init\_\_(**kwargs)
b = Button(text='Z', size_hint(.2, .2))
self.add_widget(b)

If you do this with the code still in your kvfile as is, it will put the z button first and then go down the list of buttons (A, B, etc.)
</StackLayoutExample>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Using only the StackLayout class, make multiple buttons using a loop

A

from kv.uix.stacklayout import StackLayout
def __init__(self, kwargs):
def super().__init__(
kwargs):
for i in range(0, 10):
Button(text=str(i + 1), size_hint(.2, .2))

If you were using dp here it would look like:
from kivy.uix.metrics import dp

size=(dp(100), dp(100))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In terms of StackLayout, what if we don’t want to stack from left to right and up to down?

Give padding (distance from wall)

Give space between

A

orientation: lr-tb
left-right
or
top-bottom

If doing this via the class
self.orientation = ‘lr-bt’

padding: (‘20dp’, ‘20dp’, ‘20dp’, ‘20dp’)
# Left, Right, Top, Bottom

spacing: ‘20dp’, ‘20dp’
# Horizontal, Vertical

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Show How you would scroll your app
We want to scroll Vertically
Make sure you can’t over scoll

A

from kivy.uix.scrollview import ScrollView

class ScrollViewExample(ScrollView):
pass

from KV file

ScrollViewExample:
StackLayoutExample:
size_hint: 1, None
height: self.minimum_height

This is calling from our StackLayoutExample btw
StackLayoutExample is a child of ScrollViewExample here so it needs indentation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Slide between your different pages of buttons

Each page is transparent, we will have to add a background to cover that up

A

PageLayoutExample:
PageLayoutExample@PageLayout:
MainWidget

You’ll notice your code <MainWidget> doesn't display on it's own unless you add "MainWidget" as a child to PageLayoutExample.</MainWidget>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly