@Karina said:
@stephen I read about args *kwargs things, but they didn't say what it is, like list or dict. Now you wrote couple words about, not article, and I got what is it๐
im glad i was able to help with * and ** .
@Karina said:
I'm reading now your buttons, and you use there ui, but didn't import it. You can access ui through scene?
The scene modulewas built off of theui` module. both, if im correct, were created for Pythonista specifically. if you go into your
Python Modules => Standard Library (3.6) => site-packages => scene.py
you will see at the top:
#import 'pythonista'
#coding: utf-8
from _scene2 import *
import _scene2
from scene_drawing import *
import math
from numbers import Number
from io import BytesIO
import ui
_scene2 is the core module
scene_drawing is aditional functionality
- then
math, numbers.Number, io.BytesIO are standard
- and the
ui module
when we used from scene import * this means everything ๐ so we dont need to import scene_drawing, math, numbers.Number, io.BytesIO or ui. scene does this for us.
If you want you can still do your own import but it will still be the same instance a that was imported inside scene so theres really no need.
@Karina said:
Also they not so explain in docs what the sender param is
wow im suprised the docs didnt cover this one.. but its really simple and ill show you an example i made of a ButtonNode i made to use with scene that hould be very close to how ui uses it.
When you create a ui.Button and set the action property and then Tap the button there are a few things that happends..
first it changes to the pressed state that changes the image to visually how you di tap it. then it calls the function/method that you set action to and passes self as the only argument.
class ui.Button(ui.view):
...
def _action(self):
# some cool stuff
self.action(self)
# more cool stuff
...
this is why your Custome action needs sender
now inside your custome action you can use sender to accesd the button you pressed
def button_tapped(sender):
sender.superview.background_color = "blue"
this changes the color of the view that contains your button. ๐
here is the example for a button in scene..
import scene
class EventHandler:
children=[]
event_loop=None
@classmethod
def add_child(cls, node):
cls.children.append(node)
if cls.event_loop == None:
cls.event_loop = node.scene
@classmethod
def count(cls):
return len(cls.children)
@classmethod
def touch_began(cls, touch):
for child in cls.children:
if cls.event_loop.point_from_scene(touch.location) in child.frame:
child.Button_Tapped()
class ButtonNode(scene.ShapeNode):
def __init__(self,
action,
name=f'ButtonNode',
bg_color='#d4d2ca',
accessoryColor='#e80000',
icon=None,
accessory='emj:Exclamation_Mark_2',
anchor_point=(0.5, 0.5),
font_family='Helvetica',
font_size=16,
parent=None,
position=(0, 0),
size=(120, 45),
corner_radius=8,
border_size=20,
borderColor='#000000',
text='',
text_color='#000000',
tint='white',
enabled=True,
*args, **kwargs):
self.x, self.y = position
self.w, self.h = size
super().__init__(
path=scene.ui.Path.rounded_rect(
self.x,
self.y,
self.w,
self.h,
corner_radius),
fill_color=bg_color,
stroke_color=borderColor,
shadow=None,
parent=parent,
*args, **kwargs)
EventHandler.add_child(self)
self.icon=self._init_textures(icon)
self.accessory=self._init_textures(accessory)
self.border_size=border_size
self.name=name + ' ' + str(EventHandler.count())
self.enabled=enabled
self.text=text if text != '' else self.name
self.tint=tint
self.text_color=text_color
self.borderColor=borderColor
self.corner_radius=corner_radius
self.showAccesory=False
self.accessoryColor=accessoryColor
self.parentNode=parent
self.position=position
self.size=size
self.button_action=action
self.anchor_point=anchor_point
self.font_family=font_family
self.font_size=font_size
self.components=dict({
'accessory':None,
'icon':None,
'label':None,
'width':None,
'height':None})
self._setup(self.icon, self.accessory, self.components)
def _init_textures(self, img):
if type(img) == str or type(img) == scene.ui.Image:
return scene.Texture(img)
else:
return None
def _setup(self, i, a, c):
if a != None:
c['accessory']=scene.SpriteNode(
texture=a,
size=(self.size[1]/4, self.size[1]/5*1.5),
position=scene.Point(-self.size[0]/2+7, self.size[1]/2-10),
parent=self,
z_position=4,
color=self.accessoryColor)
if i != None:
c['icon']=scene.SpriteNode(
texture=i,
size=scene.Size(self.size[1]/2, self.size[1]/2),
position=scene.Point(self.w/2 - self.size[1]/3 , 0),
parent=self,
z_position=9)
if self.text:
c['label']=scene.LabelNode(
text=self.text,
font=(self.font_family, self.font_size),
position=scene.Point(0 , 0),
anchor_point=(0.5, 0.5),
color=self.text_color,
parent=self,
z_position=10)
def Button_Tapped(self):
if self.components['accessory'].alpha > 0:
self.components['accessory'].alpha = 0
if self.enabled:
self.button_action(self)
#---------------------------------------------------------------------------
def my_button_action(sender):
print(f'{sender.name}: {sender.text}..')
class main(scene.Scene):
def setup(self):
self.background_color='#f2f2f2'
self.my_button_up=ButtonNode(
text='',
parent=self,
position=scene.Point(self.size[0]/2, self.size[1]/2+100),
action=my_button_action)
self.my_button_right=ButtonNode(
text='right',
parent=self,
position=scene.Point(self.size[0]/2+100, self.size[1]/2),
action=my_button_action)
self.my_button_down=ButtonNode(
text='down',
parent=self,
position=scene.Point(self.size[0]/2, self.size[1]/2-100),
action=my_button_action)
self.my_button_left=ButtonNode(
text='left',
parent=self,
position=scene.Point(self.size[0]/2-100, self.size[1]/2),
action=my_button_action)
def touch_began(self, touch):
EventHandler.touch_began(touch)
scene.run(main())