Forum Archive

Shield. A class for blocking touch events.

polymerchm
# Sheild 
# covers the designated view to suppress touch events. 

import ui

class Shield(object):
    def __init__(self,view): # view is view to cover
        self.view = view
        self.shield = ui.View(frame=view.bounds,background_color=(1.0, 1.0, 1.0, 0.7),flex='WH')
        self.shield.frame = self.view.frame


    def conceal(self): # ui seems to ignore multiple requests to add the same subview.  Good.
        if self.view.superview:
            self.view.superview.add_subview(self.shield)
        else: #its the root view
            self.view.add_subview(self.shield)

    def reveal(self): #ui is also happy to ignore requests to remove an non-existant subview.  Also good.
        if self.view.superview:
            self.view.superview.remove_subview(self.shield)
        else: #see above
            self.view.remove_subview(self.shield)

    def delete(self):
        del self.shield

if __name__ == '__main__':

    class LDS(ui.ListDataSource):
        def __init__(self,items=None):
            super(LDS,self).__init__(items)

        def tableview_did_select(self,tv,section,row):
            t1.text = self.items[row]

    def shieldsUp(sender):
        s1.conceal()
        s2.conceal()
    def shieldsDown(sender):
        global s1
        s1.reveal()
        s2.reveal()


    v = ui.View(frame=(0,0,600,600),name='main')

    tv = ui.TableView(name='tv')
    lds = LDS(items='one two three four five'.split())
    tv.data_source = tv.delegate = lds
    v.add_subview(tv)
    tv.frame=(10,10,400,500)

    b1 = ui.Button(frame=(450,10,100,50),
                   background_color = 'white',
                   title = 'Shields up!',
                   )
    b2 = ui.Button(frame=(450,100,100,50),
                   background_color = 'white',
                   title = 'Shields down',
                   )
    b1.action = shieldsUp
    b2.action = shieldsDown

    v.add_subview(b1)
    v.add_subview(b2)

    t1 = ui.TextView()
    v.add_subview(t1)
    t1.frame=(100,550,200,30)
    t1.text = 'READY'
    s1 = Shield(tv)
    s2 = Shield(t1)

    v.present('sheet')
    v.wait_modal()
    s1.delete()
    s2.delete()
reefboy1

What does a class mean?

ccc

Classes are described here https://docs.python.org/2.7/tutorial/classes.html

polymerchm

Short description. A template for a python object. Defines its instance variables and method.

Learn this fast or you are not really using Python to its ultimate potential.

ccc

Let's make sure that Reefboy1 masters functions and data structures (lists, dicts, sets) before he embarks on the advanced topic of classes.

polymerchm

Gotcha.

reefboy1

Ok I've got it down, ive made this

class Cat(object):
    type = 'fuzzy'
    name = 'cuddlez'

In the shell you can do Cat.type or Cat.name and they will pop up. I believe I did this correctly, did I?

polymerchm

@reefboy1

should be

class Cat(object):
     def __init__(self):
          self.type = 'fuzzy'
          self.name = 'cudelez'

myPet = Cat()
print myPet.type      [would print fuzzy]
print myPet.name    [would print cudelez]

yourPet = Cat()
yourPet.type = "ferocious'
yourPet.name = "tiger'


#A SIMPLE TUTORIAL FOR CLASSES

#A class is a "template" for a python object.  
#the object it builds is a "instance" of that class.  

#Objects have attributes (variables) and methods (functions)
#Each instance of a class has its own unique copies of its instances and methods 
#that only operatate on that particular instance of the class.  This is at the core of object oriented programming: 
# protecting instances for accidently affecting other instances

#for example:

class Adder():                # this defines the classes name.  

# What else can go in the  parenthesis is for another day.

    def __init__(self):         # this is the method that creates this instance and does anything
                         # needed when it is created.  
                         # "self" stands in for the actual instance that is created.
                         # any method in the class will get this as it first parameter
                                   # the init method can have parameters besides the required self
                                  # typically used to set up values or attributes at the creation of the instance.

        self.sum = 0         # sum is an attribute (variable).  the "self." in front says its
                         # an attribute of the instance.  Without the "self." in front, 
                         # its local to the methdod and disappears after the method runs.
                          # all methods in the instance can assess this attribute.

    def add(self,value):          # this method will increment the sum attribute of the instance .  Note
                          # the first parameter is self, even if down below, its not in the call.
        self.sum += value

    def reset(self):                        # set the attribute sum to zero for ths instance.
        self.sum = 0

    def retrieve(self):                 # returns the current value of the sum attribute 
        return self.sum

    def display(self):                  # prints out the current value of the 
        print "The current sum = {}".format(self.sum)

if __name__ == '__main__':      # this test only runs the code below it if this file is run directly.
                            # mainly for testing a class.  to have another python file have access to
                            # class with explicitly writing it again, you use the import statement

    myAdder = Adder()                       # creates an instance of the class Adder
                                                                                # note the absence of the self parameter in the call.
                                                                                # python handles that for you

    yourAdder = Adder()                 # creates a second different instance
    myAdder.add(10)                         # increments the first one by 10
    yourAdder.add(5)                        # increments the other one by 5
    myAdder.add(-5)
    myAdder.display()                       # prints out the current value of sum in the first instance
    yourAdder.display()                 # ditto for the second instance.
    myAdder.reset()
    myAdder.display()

reefboy1

Oh I see, cool. Thanks

JonB

Poly, your shield example really needs some sound effects :)