Rapid GUI Programming with Python and Qt Classes

  • Slides: 31
Download presentation
Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed

Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed 1

Classes and Modules Python fully supports procedural and object-oriented programming The syntax for creating

Classes and Modules Python fully supports procedural and object-oriented programming The syntax for creating a class is simple: class. Name(base_classes): suite 2

Creating Instances • Python has the __new__() special method which is called to construct

Creating Instances • Python has the __new__() special method which is called to construct an object, • the __init__() special method which is called to initialize a newly constructed object. • When an object is about to be garbage-collected its __del__() special method is called, with self as its only argument. We will create one that stores a string (the name of a kind of chair) and a number (how many legs the chair has): class Chair(object): """This class represents chairs. """ def __init__(self, name, legs=4): self. name = name self. legs = legs 3

Creating Instances To create an instance of a class, we use the following syntax:

Creating Instances To create an instance of a class, we use the following syntax: instance = class. Name(arguments) for example: chair 1 = Chair("Barcelona") chair 2 = Chair("Bar Stool", 1) Since the attributes are public, they can be read or assigned to using the dot (. ) operator; for example: print chair 2. name will print “Bar Stool”, and chair 1. legs = 2 will change chair 1’s legs attribute’s value from 4 to 2. 4

Methods and Special Methods class Rectangle(object): def __init__(self, width, height): self. width = width

Methods and Special Methods class Rectangle(object): def __init__(self, width, height): self. width = width self. height = height def get. Width(self): return self. width def set. Width(self, width): self. width = width def get. Height(self): return self. height def set. Height(self, height): self. height = height def area(self): return self. get. Width() * self. get. Height() 5

Methods and Special Methods rect = Rectangle(50, 10) print rect. area() rect. set. Width(20)

Methods and Special Methods rect = Rectangle(50, 10) print rect. area() rect. set. Width(20) # Prints "500" 6

Methods and Special Methods property() function class Rectangle(object): def __init__(self, width, height): self. width

Methods and Special Methods property() function class Rectangle(object): def __init__(self, width, height): self. width = width self. height = height def _area(self): return self. width * self. height area = property(fget=_area) 7

Methods and Special Methods rect = Rectangle(5, 4) print rect. width, rect. height, rect.

Methods and Special Methods rect = Rectangle(5, 4) print rect. width, rect. height, rect. area rect. width = 6 # Prints (5, 4, 20) 8

Methods and Special Methods def _width(self): return self. __width def _set. Width(self, width): #

Methods and Special Methods def _width(self): return self. __width def _set. Width(self, width): # Perform some computation self. __width = property(fget=_width, fset=_set. Width) 9

Methods and Special Methods class Rectangle(object): def __init__(self, width, height): self. __width = width

Methods and Special Methods class Rectangle(object): def __init__(self, width, height): self. __width = width self. __height = height def _area(self): return self. __width * self. __height area = property(fget=_area) def _height(self): return self. __height def _set. Height(self, height): self. __height = property(fget=_height, fset=_set. Height) 10

Methods and Special Methods def _width(self): return self. __width def _set. Width(self, width): self.

Methods and Special Methods def _width(self): return self. __width def _set. Width(self, width): self. __width = property(fget=_width, fset=_set. Width) def __cmp__(self, other): return cmp(self. area, other. area) def __nonzero__(self): return self. __width or self. __height def __repr__(self): return "Rectangle(%d, %d)" % (self. __width, self. __height) 11

Methods and Special Methods print ("Starting. . n") rect 1 = Rectangle(4, 5) print

Methods and Special Methods print ("Starting. . n") rect 1 = Rectangle(4, 5) print ("Area is [", rect 1. area, "]") print ("Set width to [ 7 ] and height to [ 2 ]") rect 1. width = 7 rect 1. height = 2 print ("Now area is [", rect 1. area, "]") print ("n. Finishing. . ") 12

Methods and Special Methods Output: Starting. . Area is [ 20 ] Set width

Methods and Special Methods Output: Starting. . Area is [ 20 ] Set width to [ 7 ] and height to [ 2 ] Now area is [ 14 ] Finishing. . 13

Methods and Special Methods Method __init__(self, args) __call__(self, args) Syntax x = X() x()

Methods and Special Methods Method __init__(self, args) __call__(self, args) Syntax x = X() x() __cmp__(self, other) x == y x<y # etc __eq__(self, other) __ne__(self, other) __le__(self, other) x == y x != y x <= y Description Initializes a newly created instance Makes instances callable, that is, turns them into functors. The args are optional. Returns -1 if self < other, 0 if they are equal, and 1 otherwise. If __cmp__() is implemented, it will be used for any comparison operators that are not explicitly implemented. Returns True if x is equal to y Returns True if x is not equal to y Returns True if x is less than or equal to y 14

Methods and Special Methods Method __lt__(self, other) __ge__(self, other) Syntax x<y x >= y

Methods and Special Methods Method __lt__(self, other) __ge__(self, other) Syntax x<y x >= y __gt__(self, other) __nonzero__(self) __repr__(self) x>y if x: pass y = eval(`x`) __str__(self) print x __unicode__(self) print x Description Returns True if x is less than y Returns True if x is greater than or equal to y Returns True if x is greater than y Returns True if x is nonzero Returns an eval()-able representation of x. Using backticks is the same as calling repr(). Returns a human-readable representation of x Returns a human-readable Unicode representation of x 15

Methods and Special Methods def __cmp__(self, other): return cmp(self. area(), other. area()) rect. A

Methods and Special Methods def __cmp__(self, other): return cmp(self. area(), other. area()) rect. A = Rectangle(4, 4) rect. B = Rectangle(8, 2) rect. A == rect. B rect. A < rect. B # True because both have the same area # False def __cmp__(self, other): if (self. width != other. width): return cmp(self. width, other. width) return cmp(self. height, other. height) 16

Methods and Special Methods def __nonzero__(self): return self. width or self. height) def __repr__(self):

Methods and Special Methods def __nonzero__(self): return self. width or self. height) def __repr__(self): return "Rectangle(%d, %d)" % (self. width, self. height) 17

Methods and Special Methods Method __float__(self) __abs__(self) __add__(self, other) __iadd__(self, other) __radd__(self, other) __mul__(self,

Methods and Special Methods Method __float__(self) __abs__(self) __add__(self, other) __iadd__(self, other) __radd__(self, other) __mul__(self, other) __imul__(self, other) __rmul__(self, other) __floordiv__(self, other) y __ifloordiv__(self, other) /= y __rfloordiv__(self, other) Syntax float(x) abs(x) x+y x +=y y+x x*y x *= y y*x x // y Method __int__(self) __neg__(self) __sub__(self, other) __isub__(self, other) __rsub__(self, other) __mod__(self, other) __imod__(self, other) __rmod__(self, other) __truediv__(self, other) x //= y __itruediv__(self, other) y // x __rtruediv__(self, other) Syntax int(x) -x x-y x -= y y-x x%y x %= y y%x x/ x y/x 18

Static Data, and Static Methods and Decorators class Balloon(object): unique_colors = set() def __init__(self,

Static Data, and Static Methods and Decorators class Balloon(object): unique_colors = set() def __init__(self, color): self. color = color Balloon. unique_colors. add(color) @staticmethod def unique. Color. Count(): return len(Balloon. unique_colors) @staticmethod def unique. Colors(): return Balloon. unique_colors. copy() 19

Static Data, and Static Methods and Decorators class Example: static. Variable = 5 print("startingn")

Static Data, and Static Methods and Decorators class Example: static. Variable = 5 print("startingn") # Access through class print (Example. static. Variable) # prints 5 # Access through instance = Example() print (instance. static. Variable) # still 5 20

Static Data, and Static Methods and Decorators # Change within instance. static. Variable =

Static Data, and Static Methods and Decorators # Change within instance. static. Variable = 6 print (instance. static. Variable) # 6 print (Example. static. Variable) # 5 # Change through class Example. static. Variable = 7 print (instance. static. Variable) # still 6 print (Example. static. Variable) # now 7 print("nfinishing") 21

Static Data, and Static Methods and Decorators class Example(object): name = "Example" @staticmethod def

Static Data, and Static Methods and Decorators class Example(object): name = "Example" @staticmethod def static(): print ("%s static() called" % Example. name) class Offspring 1(Example): name = "Offspring 1" 22

Static Data, and Static Methods and Decorators class Offspring 2(Example): name = "Offspring 2"

Static Data, and Static Methods and Decorators class Offspring 2(Example): name = "Offspring 2" @staticmethod def static(): print ("%s static() called" % Example. name) print("startingn") Example. static() # prints Example Offspring 1. static() # prints Example Offspring 2. static() # prints Offspring 2 print("nfinishing“) 23

Static Data, and Static Methods and Decorators class Example: name = "Example" @classmethod def

Static Data, and Static Methods and Decorators class Example: name = "Example" @classmethod def static(cls): print ("%s static() called" % cls. name) class Offspring 1(Example): name = "Offspring 1" pass 24

Static Data, and Static Methods and Decorators class Offspring 2(Example): name = "Offspring 2"

Static Data, and Static Methods and Decorators class Offspring 2(Example): name = "Offspring 2" @classmethod def static(cls): print ("%s static() called" % cls. name) print("startingn") Example. static() # prints Example Offspring 1. static() # prints Offspring 1 Offspring 2. static() # prints Offspring 2 print("nfinishing") 25

Inheritance and Polymorphism class Item(object): def __init__(self, artist, title, year=None): self. __artist = artist

Inheritance and Polymorphism class Item(object): def __init__(self, artist, title, year=None): self. __artist = artist self. __title = title self. __year = year def artist(self): return self. __artist def set. Artist(self, artist): self. __artist = artist def title(self): return self. __title 26

Inheritance and Polymorphism def title(self): return self. __title def set. Title(self, title): self. __title

Inheritance and Polymorphism def title(self): return self. __title def set. Title(self, title): self. __title = title def year(self): return self. __year def set. Year(self, year): self. __year = year def __str__(self): year = "" if self. __year is not None: year = " in %d" % self. __year return "%s by %s%s" % (self. __title, self. __artist, year) 27

Inheritance and Polymorphism class Painting(Item): def __init__(self, artist, title, year=None): super(Painting, self). __init__(artist, title,

Inheritance and Polymorphism class Painting(Item): def __init__(self, artist, title, year=None): super(Painting, self). __init__(artist, title, year) # item. __init__(self, artist, title, year) class Sculpture(Item): def __init__(self, artist, title, year=None, material=None): super(Sculpture, self). __init__(artist, title, year) self. __material = material 28

Inheritance and Polymorphism def material(self): return self. __material def set. Material(self, material): self. __material

Inheritance and Polymorphism def material(self): return self. __material def set. Material(self, material): self. __material = material def __str__(self): material. String = "" if self. __material is not None: material. String = " (%s)" % self. __material return "%s%s" % (super(Sculpture, self). __str__(), material. String) 29

Inheritance and Polymorphism class Title(object): def __init__(self, title) self. __title = title def title(self):

Inheritance and Polymorphism class Title(object): def __init__(self, title) self. __title = title def title(self): return self. __title return "%s%s" % (super(Sculpture, self). __str__(), material. String) 30

Inheritance and Polymorphism items = [] items. append(Painting("Cecil Collins", "The Poet", 1941)) items. append(Sculpture("Auguste

Inheritance and Polymorphism items = [] items. append(Painting("Cecil Collins", "The Poet", 1941)) items. append(Sculpture("Auguste Rodin", "Naked Balzac", 1917, "plaster")) items. append(Title("Eternal Springtime")) for item in items: print item. title() 31