Galaxy App Flashcards

1
Q

How do you get the current size of the screen?

A

class MainWidget(Widget):
perspective_point_x = NumericProperty(0)
perspective_point_y = NumericProperty(0)

def \_\_init\_\_(self, **kwargs):
    super(MainWidget, self).\_\_init\_\_(**kwargs)
  #  print("INIT W: " + str(self.width) + " H: " + str(self.height))
    pass

def on_size(self, *args):
print(“INIT W: “ + str(self.width) + “ H: “ + str(self.height))

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

How do you create a integer variable that you can change later?

A

from kivy.properties import NumericProperty

NumericProperty(0)

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

You’ve create the below class attributes/class variables (Not INSTANCE variables)

perspective_point_x = 0
perspective_point_y = 0

Allow them to be updated.

They should update after the instance is ran, and upon when the size of the window changes. X should be half the size of the window’s width (so center) and the height should be 75 percent the window’s height

A

from kivy.properties import NumericProperty

perspective_point_x = NumericProperty(0)
perspective_point_y = NumericProperty(0)

def on_size(self, *args):
self.perspective_point_x = self.width/2
self.perspective_point_y = self.height * .075

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

Describe the on_parent method and its parameters

A

def on_parent(self, widget, parent)

This method is called whenever a widget is created or taken out of its parent.
widget - refers to the widget that it’s in (In our case, it’s the MAINWIDGET)
parent - refers to the parent of the widget (in our case it’s the main window)

All together, this means that whenever the MAINWIDGET is added to the window or taken out of it, this method will be called.

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

How do you do something depending on when something like a variable you set changes?

like this:

perspective_point_x = NumericProperty(0)

A

def on_perspective_point_x(self, widget, value):
print(“PX: “ + str(value))

This is very similar to binding

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

Can you call methods in __init__?
Why or why not?

A

Yes, if you call a method it will perform that method.

This is because the class is read when the class is created.

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

Create a class that calls a method that creates the line upon initialization. THIS IS FOR READABILITY AND ORGANIZATION ONLY

A

def __init__(self, kwargs):
super(MainWidget, self).__init__(
kwargs) self.init_vertical_lines()

def init_vertical_lines(self):
    with self.canvas:
        Color(1, 1, 1)
        Line(points=[100, 0, 100, 100])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What should you do rather than update the point on a line from on_size directly for readability and organization?

A

Have it call another variable that updates the size. Just name the method something that indicates what it’s doing directly

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

Create a variable that updates via a method

A

from kivy.properties import Clock

Clock.schedule_interval(self.update, 1/60)

def update(self, dt):
thing happens

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

How do you make a touch pad for your app?

A

this is for when finger on screen

def on_touch_down(self, touch):
if touch.x < self.width/2:
print(‘<-‘)
else:
print(‘->’)

def on_touch_up(self, touch):
    print('up')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Have something happen according to keyboard clicks

A

from kivy.core.window import Window

self._keyboard = Window.request_keyboard(self.keyboard_closed, self)
self._keyboard.bind(on_key_down=self.on_keyboard_down)
self._keyboard.bind(on_key_up=self.on_keyboard_up)

def keyboard_closed(self):
    self._keyboard.unbind(on_key_down=self.on_keyboard_down)
    self._keyboard.unbind(on_key_up=self.on_keyboard_up)
    self._keyboard = None

def on_keyboard_down(self, keyboard, keycode, text, modifiers):
    if keycode[1] == 'left':
        print('left')    
    elif keycode[1] == 'right':
        print('right')
    elif keycode[1] == 'up':
        print('up')
    elif keycode[1] == 'down':  
        print('down')     
    return True 
def on_keyboard_up(self, keyboard, keycode):
    pass

keycode[1] is for string representation of the key
keycode[0] is for digit representation of the key

on_key_up=self.on_keyboard_up <- this means when the on_key_up event takes place it will run the on_keyboard_up method. You don’t have to put self here, just the reference to the method

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

How do you do something depending on the device that’s running your app?

A

from kivy import platform

in the __init__ method
if self.is_desktop
def is_desktop(self):
put keyboard bindings here

if platform in (‘linux’, ‘win’, ‘macosx’)
return True
return False

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

How would you add methods from a module you created into your MainWidget class?

A

make sure your module is called transforms.py

class MainWidget(Widget):
from transforms import (name all methods)

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

How do you create a main menu?

A

In the same file as your project create menu.py and menu.kv

class MenuWidget(RelativeLayout):
pass

<MenuWidget>:
Label:
text: "TITLE"
pos_hint: { "center_x": .5, "center_y": .6 }
Button:
text: "OK"
pos_hint: { "center_x": .5, "center_y": .4 }
size_hint: .2, .1

IN YOUR GALAXY.KV FILE
#:import menu menu
MainWidget:
<MainWidget>:
MenuWidget:

IN YOUR MAIN.PY ABOVE YOUR MAINWIDGET CLASS AND BELOW YOUR IMPORTS

Builder.loadfile("menu.kv")
</MainWidget></MenuWidget>

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

Where should you put variables that change using kivy properties at?

A

before initialization. This is because they lose their properties when you put them in init, that essentially makes them python trash

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

What do you use to call a method from a specific action?

A

bind

For instance the below is the best way to bind what happens on press, rather than setting the method as on_press value without bind. If we did the latter, it would be called immediately upon initialization and that’s it.

button = Button(text=’Click Me’)
button.bind(on_press=self.change_text)

def_change_text(self, instance):
print(‘Button was pressed’)