Galaxy App Flashcards
How do you get the current size of the screen?
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 do you create a integer variable that you can change later?
from kivy.properties import NumericProperty
NumericProperty(0)
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
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
Describe the on_parent method and its parameters
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 do you do something depending on when something like a variable you set changes?
like this:
perspective_point_x = NumericProperty(0)
def on_perspective_point_x(self, widget, value):
print(“PX: “ + str(value))
This is very similar to binding
Can you call methods in __init__?
Why or why not?
Yes, if you call a method it will perform that method.
This is because the class is read when the class is created.
Create a class that calls a method that creates the line upon initialization. THIS IS FOR READABILITY AND ORGANIZATION ONLY
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])
What should you do rather than update the point on a line from on_size directly for readability and organization?
Have it call another variable that updates the size. Just name the method something that indicates what it’s doing directly
Create a variable that updates via a method
from kivy.properties import Clock
Clock.schedule_interval(self.update, 1/60)
def update(self, dt):
thing happens
How do you make a touch pad for your app?
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')
Have something happen according to keyboard clicks
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 do you do something depending on the device that’s running your app?
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 would you add methods from a module you created into your MainWidget class?
make sure your module is called transforms.py
class MainWidget(Widget):
from transforms import (name all methods)
How do you create a main menu?
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>
Where should you put variables that change using kivy properties at?
before initialization. This is because they lose their properties when you put them in init, that essentially makes them python trash
What do you use to call a method from a specific action?
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’)