<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>OOP :: CS2 course</title><link>https://robark.gitlab.io/oop/index.html</link><description>Chapter 4 OOP Object Oriented Programming</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Sat, 12 Dec 2020 00:09:38 +0000</lastBuildDate><atom:link href="https://robark.gitlab.io/oop/index.xml" rel="self" type="application/rss+xml"/><item><title>Intro</title><link>https://robark.gitlab.io/oop/intro/index.html</link><pubDate>Thu, 03 Dec 2020 22:49:30 +0000</pubDate><guid>https://robark.gitlab.io/oop/intro/index.html</guid><description>Lesson 10: OOP (Object Oriented Programming) class Dog: alive=0 def __init__(self, color='white', breed='mutt'): print(' a dog is born') self.color=color self.breed=breed Dog.alive += 1 def speak(self): print('ruf ruf') def __str__(self): return(f'this dog is a {self.color} {self.breed}') def __del__(self): print (f'the {self.color} {self.breed} dog died') Dog.alive -= 1 def birth(): fido3=Dog() print('fido3 in func ',id(fido3)) print('in func birth',Dog.alive) fido1=Dog() #init is called when a dog is created fido2=Dog('black', 'lab') fido1.speak() fido2.speak() print(fido1) print(fido2) print('before birth func call',Dog.alive) # 2 birth() #print fido3 #this line fails since fido3 no longer exists after func birth() (it was garbage collected) print('after birth func call',Dog.alive) # 2 input() A=[] for x in range(10): A.append(Dog()) print('after loop',Dog.alive)</description></item><item><title>Inheritance</title><link>https://robark.gitlab.io/oop/inherit/index.html</link><pubDate>Fri, 04 Dec 2020 21:16:45 +0000</pubDate><guid>https://robark.gitlab.io/oop/inherit/index.html</guid><description>Lesson 11: Inheritance class Dog: alive=0 def __init__(self, color='white', breed='mutt'): self.color=color self.breed=breed Dog.alive=Dog.alive+1 def __del__(self): Dog.alive -= 1 def speak(self): print('Wuf Wuf') def __str__(self): return (f'this dog is a {self.color} {self.breed}') class Puppy(Dog): def __init__(self, color='white', breed='mutt' ,wormed=True): #choose one of the following 2 lines Dog.__init__(self,color,breed) #super().__init__(color,breed) #notice no self passed as first arg for super(). self.dewormed=wormed def speak(self): #if not defined puppies will speak Wuf Wuf as the parent Dog print('ruf') def __str__(self): return (f'this Puppy is a {self.color} {self.breed}') def birth(): fido3=Dog('grey','shepard') #local var fido4=Dog() #local var print (f'dogs alive in func = {Dog.alive}') #at the end of this func all local vars are garbage collected (they are deleted so the del func is called) fido1=Dog() #init is called when a dog is created with default args of white and mutt fido2=Dog('black', 'lab') fido1.speak() fido2.speak() print(fido1) print(fido2) print('before func',Dog.alive) birth() print('after func',Dog.alive) fido5=Dog() print(Dog.alive) pup1=Puppy() pup2=Puppy('brown', 'pointer') pup1.speak() print(f'after puppies are born {Dog.alive}') print(pup1) class MyApp(Fl_Window): def __init__(self, w, h, label): #x,y,w,h,l w,h,l Fl_Window.__init__(self, w, h, label) self.begin() self.but = Fl_Button(95, 5, 140, 50, "Not clicked yet") self.but.callback(self.but_cb) self.but.color(FL_BLUE) self.inp = Fl_Input(95, 90, 140, 50,"Input") #x,y,w,h,label self.end() self.show() def but_cb(self, wid): wid.label(self.inp.value()) #wid is the self.but self.but.label(self.inp.value()) #wid is the self.but if __name__=='__main__': Fl.scheme("plastic") app = MyApp(450, 155, "Widget communication") Fl.run()</description></item><item><title>Convert to OOP</title><link>https://robark.gitlab.io/oop/convert/index.html</link><pubDate>Tue, 08 Dec 2020 23:25:48 +0000</pubDate><guid>https://robark.gitlab.io/oop/convert/index.html</guid><description>Lesson 12: Converting Procedural code to OOP From callbacks and images page
from fltk import * #OOP class Imgwin(Fl_Window): def __init__(self, width, label, img): self.pic=Fl_PNG_Image(img) ar = self.pic.w()/self.pic.h() #aspect ratio bh = int(width/ar) #correct box height for width of 300 Fl_Window.__init__(self, width , bh, label) self.begin() self.box=Fl_Box(0, 0, width, bh) self.end() self.pic= self.pic.copy(self.box.w(), self.box.h()) self.box.image(self.pic) app=Imgwin(300,'Cat image','cat.png') app.show() Fl.run() Next example: From Slotmachine page</description></item><item><title>Extending</title><link>https://robark.gitlab.io/oop/extending/index.html</link><pubDate>Thu, 10 Dec 2020 22:42:57 +0000</pubDate><guid>https://robark.gitlab.io/oop/extending/index.html</guid><description>Lesson 13: Converting to OOP and extending existing widgets by overriding draw and handle methods Converted and extended previous code from imageviewer example
from fltk import * import os class mybutton(Fl_Button): def __init__(self, x, y, w, h, label=None): Fl_Button.__init__(self ,x , y, w, h, label) def handle(self, event): retval = super().handle(event) #must call base class handle method for event if event == FL_ENTER: self.color(FL_BLUE) self.redraw() return 1 elif event == FL_LEAVE: self.color(FL_BACKGROUND_COLOR) self.redraw() return 1 else: return retval #return base class retval if we did not handle it class mybox(Fl_Box): def __init__(self, x, y, w, h): Fl_Box.__init__(self, x,y,w,h) self.pic=None def setimage(self, img): self.pic=img #store original image self.image(img) #display image self.redraw() #marks widget as needing to have draw method called def draw(self): super().draw() if self.pic!=None: self.image(self.pic.copy( self.w(), self.h())) #notice .copy (resize) always modifies the original image class imgviewer(Fl_Window): def __init__(self, x=0, y=0, pw=700, bw=50): self.pw=pw #need self for next_cb method self.ind=-1 self.files=self.get_filenames() self.ww=pw+(2*bw) #window width wh=300 #arbitrary value Fl_Window.__init__(self, x, y, self.ww, wh,'pyFltk Imageviewer') self.begin() self.lbut=mybutton(0,0,bw,wh,'@&lt;') self.lbut.callback(self.next_cb,-1) self.lbut.tooltip('Middle click to select new directory') self.lbut.shortcut(FL_ALT|FL_Left) self.picbox=mybox(bw,0,pw,wh) self.rbut=mybutton(bw+pw,0,bw,wh,'@&gt;') self.rbut.callback(self.next_cb,1) self.rbut.tooltip('Middle click to select new directory') self.rbut.shortcut(FL_ALT|FL_Right) self.end() self.resizable(self.picbox) def next_cb(self,wid,n): if Fl.event_button() == FL_MIDDLE_MOUSE: self.files=[] #with self. can now use =[] instead of .clear() self.files.extend(self.get_filenames()) self.ind = self.ind + n # n is +1 for right -1 for left if len(self.files)==0: fl_alert('No photos in directory') return fname=self.files[self.ind%len(self.files)] if fname[-4:]=='.png': img=Fl_PNG_Image(fname) else: img=Fl_JPEG_Image(fname) ar=img.w()/img.h() h=int(self.pw/ar) self.size(self.ww,h) #resizing window causes picbox to also resize #self.picbox.image(img.copy(self.pw,h)) #self.picbox.redraw() #use setimage from mybox class instead self.picbox.setimage(img) def get_filenames(self): #notice variables in this function do not require self. #because nothing is required to be persistant in the class d=fl_dir_chooser('Pick a directory to view photos','') if d==None: # Cancel clicked return [] fnames=os.listdir(d) picnames=[] for name in fnames: if name[-4:] in ('.png','.jpg','jpeg'): picnames.append(os.path.join(d,name)) return picnames if __name__=='__main__': Fl.scheme('plastic') app=imgviewer() app.show() Fl.run()</description></item><item><title>Draggable Widget</title><link>https://robark.gitlab.io/oop/drag/index.html</link><pubDate>Sat, 12 Dec 2020 00:09:38 +0000</pubDate><guid>https://robark.gitlab.io/oop/drag/index.html</guid><description>Lesson 14: Creating a draggable widget This code was Inspired by Erco’s FLTK Cheat Page and pyFltk test programs.
from fltk import * class tux(Fl_Box): def __init__(self,pic, x, y, w, h): Fl_Box.__init__(self, x, y, w, h) self.image(pic.copy(w,h)) self.dx=0 #offsets to top corner of box self.dy=0 def handle(self, event): r = super().handle(event) if event==FL_DRAG: X= Fl.event_x() Y= Fl.event_y() self.position(X-self.dx, Y-self.dy) self.parent().redraw() return 1 elif event==FL_PUSH: self.dx= Fl.event_x() - self.x() self.dy= Fl.event_y() - self.y() return 1 elif event==FL_RELEASE: return 1 else: return r class win(Fl_Window): def __init__(self, w, h): Fl_Window.__init__(self, w, h) img = Fl_PNG_Image('tux.png') self.cartoon = tux(img,0,0,100,150) self.end() app=win(800,600) app.show() Fl.run()</description></item></channel></rss>