Kivy 3 Flashcards

1
Q

Create an interface that has a button on the left that says ‘click me’ and a label that says ‘hello’
When you click the button change the label to something else

A

WidgetsExample:

<WidgetsExample>:
cols: 3
Button:
text: 'Click here"
on_press: root.on_button_click()
Label:
text: root.mytext

from kivy.properties import StringProperty
class WidgetsExample(GridLayout):
my_text = StringProperty('Hello')
def on_button_click(self):
print('Button Clicked')
self.my_text = 'You Clicked!'

We use root.on_button_click() instead of self, because self would refer to the button class, we need to go to the top level by using 'root'
'Button Clicked' is info sent to the command line.

StringProperty - allows your string to change immediately unlike just adding 'Hello'

we don't add self to self.mytext because kivy does this for us when the class is called, if we defined it earlier it would be redundant. Mucho confusing, but oh well.
</WidgetsExample>

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

WidgetsExample:

<WidgetsExample>:
cols: 3
Button:
text: 'Click here"
on_press: root.on_button_click()
Label:
text: root.mytext

from kivy.properties import StringProperty
class WidgetsExample(GridLayout):
my_text = StringProperty('Hello')
def on_button_click(self):
print('Button Clicked')
self.my_text = 'You Clicked!'

We use root.on_button_click() instead of self, because self would refer to the button class, we need to go to the top level by using 'root'
'Button Clicked' is info sent to the command line.

StringProperty - allows your string to change immediately unlike just adding 'Hello'

we don't add self to self.mytext because kivy does this for us when the class is called, if we defined it earlier it would be redundant. Mucho confusing, but oh well.
</WidgetsExample>

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

How do you add different fonts to your project?

The file name is Lcd.ttf
This will be a bit small, make it bigger
change the color

A

Just add the folder to your apps directory

in you Label:
font_name: ‘Lcd.ttf’
font_size: ‘80dp’
color: 1, .5, 1, 1

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

Create a button that when clicked on will show that it’s activated, when you press it again it should deactivate the button

When it’s toggled on it should do something

A

ToggleButton:
text: ‘toggle’
on_state: root.on_toggle_button_state(self)

class WidgetsExample(GridLayout):
def on_toggle_button_state(self, widget):
print(‘toggle state’ + widget.state)
if_widget.state == ‘normal’:
widget.text = ‘OFF’
else:
widget.text = ‘ON’

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

Try toggling on and off your counter button

How do you grey out your button?

A

just add a class variable called count_enabled and set it to false,
add conditions from there
or
count_enabled = BooleanProperty(False)

from kivy.properties import BooleanProperty, StringProperty
disabled = True
or
disabled = not root.count_enabled

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

Create a switch

A

Switch:
on_active: root.on_witch_active(self)

def on_switch_active(self, widget):
print(f”Switch: {srt(widget.active)}”)

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

Create a slider that goes from 0 to 100, the default value should be 50. The slider should also be vertical.
The slider should do something according to it’s values (remember to set it to integer, normally it’s a float)

A

Slider:
min: 0
max: 100
value: 50
orientation: ‘vertical’
on_value: root.on_slider_value(self)

def on_slider_value(self, widget):
print(‘Slider: ‘ + str(int(widget.value))

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

What would you use to make a variable for your slider that you can use to update a label with it’s number all in the kv file.

How would you make the switch on when the program opens

A

Slider:
id: my_slider

Label:
text: str(my_slider.value)

Switch:
active: True

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

Your widgets are all in GridLayout, but you would like to create a progress bar that follows your slider that goes from 0 100. This progress bar should be under your Label. The bar should start at 25, and then make it follow the progressbar next

A

Slider:
id: my_slider
orienatation: ‘vertical’
on_value: root.value_activity(self)
disabled: not my_switch.active
BoxLayout:
Label:
text: str(int(my_slider.value))
font_name: ‘Lcd.ttf’
font_size: ‘80dp’
color: .2, .5, 1, 1

ProgressBar:
max: 100
value: 25 to make it follow your slider value: int(my_slider.value)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Make it to where you can add text to your program.
Give it a default text.
You should only be able to put in one line of text
When you press enter

Add that text to a label

A

TextInput:
id: my_text
text: ‘foo’
multiline: False
on_text_validate: root.on_text_validate(self)

Label:
text: ‘Your name is’ + root.on_text_validate(self)

IN YOUR CLASS
text_input_string = StringProperty(‘foo’)
def on_text_validate(self, widget):
self.text_input_string = widget.text

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

Create a Switch

A

Switch:
on_active: root.on_witch_active(self)

def on_switch_active(self, widget):
print(f”Switch: {srt(widget.active)}”)

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

Add three images of the cake in a row, use the grid layout

The middle should be allowed to stretch around

The third should fill it’s whole section

A

ImagesExample:
ImagesExample@GridLayout:
cols: 3
Image:
source: ‘images/cake.jpg’
Image:
source: ‘images/cake.jpg’
allow_stretch: True
Image:
source: ‘images/cake.jpg’
allow_stretch: True
keep_ratio: False

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

What is the difference between jpg and png?

A

jpg - no transparency but can compress better

png - transparency

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

How would we put multiple screens into our project?

A

This is known as ‘navigation’ and you can learn more about it looking up ‘screen manager kivy’ in google

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