Commits

3k committed 513a19c

added the App class

  • Participants
  • Parent commits a4ca077

Comments (0)

Files changed (4)

 -----
 
 * Added -c option to console script
+* New App class
 
 1.1.0
 -----

File pilpal/__init__.py

 # encoding: utf-8
-# 
+#
 # This file is part of pilpal.
 #
 # pilpal - Simple and quick graphics programming using Python Imaging Library
 __author__ = "Károly Kiripolszky"
 __version__ = "1.1.1"
 
-__all__ = ["pilpal", "geom"]
+__all__ = ["pilpal", "apppil", "geom"]
 
-from pilpal import *
+from . import *
 

File pilpal/apppil.py

+# encoding: utf-8
+#
+# This file is part of pilpal.
+#
+# pilpal - Simple and quick graphics programming using Python Imaging Library
+# Copyright (C) 2013  Károly Kiripolszky
+#
+# pilpal is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# pilpal is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with pilpal.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+import Tkinter
+
+import Image
+import ImageDraw
+import ImageTk
+
+
+class App(Tkinter.Tk):
+
+    DEF_MODE = "RGB"
+    DEF_BGCOLOR = "white"
+    DEF_FPS = 30
+
+    _label = None
+    _im = None
+    _draw = None
+
+    _draw_delay = 1
+
+
+    def __init__(self, title, *args, **kwargs):
+        Tkinter.Tk.__init__(self, *args, **kwargs)
+        self.title(title)
+        self._label = Tkinter.Label(self)
+        self._label.pack()
+        self._label.phim = None
+
+        props = self.__class__.__dict__
+        if "size" in props:
+            bgcolor = props["bgcolor"] if "bgcolor" in props \
+                else self.DEF_BGCOLOR
+            mode = props["mode"] if "mode" in props else self.DEF_MODE
+            self.create_image(self.__class__.__dict__["size"], bgcolor, mode)
+        self.set_fps(props["fps"] if "fps" in props else self.DEF_FPS)
+
+        self.setup()
+        self._update()
+
+    def create_image(self, size, bgcolor=None, mode=None):
+        mode = mode if mode is not None else self.DEF_MODE
+        bgcolor = bgcolor if bgcolor is not None else self.DEF_BGCOLOR
+        self._im = Image.new(mode, size, bgcolor)
+        self._draw = ImageDraw.Draw(self._im, mode)
+
+    def get_canvas(self):
+        return self._draw
+
+    def clear_image(self, bgcolor=None):
+        if (self._im is None) or (self._draw is None):
+            raise AttributeError
+        self._draw.rectangle((0, 0) + self._im.size,
+            fill=bgcolor if bgcolor is not None else self.DEF_BGCOLOR)
+        return True
+
+    clear = clear_image
+
+    def set_fps(self, fps):
+        self._draw_delay = int(1000.0 * (1.0 / fps))
+
+    def on_key(self, key, fn):
+        self.bind_all(key, lambda event: fn())
+
+    def setup(self):
+        pass
+
+    def _update(self):
+        self.update()
+        if self._label.phim is None:
+            self._label.phim = ImageTk.PhotoImage(self._im)
+        phim = self._label.phim
+        phim.paste(self._im)
+        self._label.config(image=phim)
+        self.after(self._draw_delay, self._update)
+
+    def update(self):
+        pass
+

File pilpal/pilpal.py

         i = 0
         for line in lines:
             more_lines = str(line).split("\n")
-            for s in more_lines:
-                if len(s.strip()) == 0:
+            for txt in more_lines:
+                if len(txt.strip()) == 0:
                     continue
-                s = s.replace("\t", "    ")
-                if s != NL:
-                    d.text((left_margin, top + (line_height * i)), s.rstrip(),
+                txt = txt.rstrip().replace("\t", "    ")
+                if txt != NL:
+                    d.text((left_margin, top + (line_height * i)), txt,
                         font=self.font_book, fill=Theme.MSG_FG)
                 i = i + 1
         return img