From 9c7df57ba8c8ea3ad5140fc90dd4a919c1043299 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Thu, 16 May 2019 21:51:20 +0300 Subject: [PATCH] Fix Golf and other games. And broke a lot of stuff. Consolidate _pil_image on PIL.Image. --- pysollib/app.py | 9 +++++++++ pysollib/images.py | 4 ++++ pysollib/stack.py | 6 ++++++ pysollib/ui/tktile/tkcanvas.py | 5 ++++- pysollib/ui/tktile/tkutil.py | 32 ++++++++++++++++++++++++++++---- 5 files changed, 51 insertions(+), 5 deletions(-) diff --git a/pysollib/app.py b/pysollib/app.py index 60f7e470..6e66b753 100644 --- a/pysollib/app.py +++ b/pysollib/app.py @@ -180,6 +180,9 @@ class Application: # the PySol mainloop def mainloop(self): + approc = self.mainproc() # setup process + approc.send(None) # and go + return try: approc = self.mainproc() # setup process approc.send(None) # and go @@ -202,6 +205,7 @@ class Application: except Exception: traceback.print_exc() game = None + sys.exit() if game: if game.id == self.opt.game_holded and game.gstats.holded: game.gstats.loaded = game.gstats.loaded - 1 @@ -237,6 +241,7 @@ class Application: traceback.print_exc() self.nextgame.id = 2 self.freeGame() + sys.exit() continue if self.nextgame.holdgame: assert self.nextgame.id <= 0 @@ -267,9 +272,11 @@ class Application: except Exception: traceback.print_exc() + sys.exit() pass finally: + sys.exit() # hide main window self.wm_withdraw() # @@ -326,6 +333,7 @@ class Application: except Exception: traceback.print_exc() self.nextgame.loadedgame = None + sys.exit() elif self.commandline.game is not None: gameid = self.gdb.getGameByName(self.commandline.game) if gameid is None: @@ -702,6 +710,7 @@ class Application: r = 1 except (Exception, TclError, UnpicklingError) as ex: traceback.print_exc() + sys.exit() cs.error = 1 # restore settings self.nextgame.cardset = self.cardset diff --git a/pysollib/images.py b/pysollib/images.py index e19cd956..e875a2aa 100644 --- a/pysollib/images.py +++ b/pysollib/images.py @@ -115,6 +115,7 @@ class Images: if ((check_w and w != self.CARDW) or (check_h and h != self.CARDH)): raise ValueError("Invalid size %dx%d of image %s" % (w, h, f)) + assert img return img def __loadBottom(self, filename, check_w=1, check_h=1, color='white'): @@ -145,6 +146,9 @@ class Images: def __addBack(self, im1, name): r = max(self.CARDW / 40.0, self.CARDH / 60.0) r = max(2, int(round(r))) + # im2 = PIL_Image(image=im1.subsample(r)) + print(type(im1)) + im1 = PIL_Image(image=im1) im2 = im1.subsample(r) self._back.append(ImagesCardback(len(self._back), name, im1, im2)) diff --git a/pysollib/stack.py b/pysollib/stack.py index 396f6234..d414b050 100644 --- a/pysollib/stack.py +++ b/pysollib/stack.py @@ -862,6 +862,12 @@ class Stack: # bottom and shade if self.images.bottom: img = self.getBottomImage() + img2 = img + try: + from pysollib.pysoltk import get_image_tk + img = get_image_tk(img) + except Exception: + img = img2 self.images.bottom['image'] = img self.images.bottom.moveTo(x, y) if self.items.bottom: diff --git a/pysollib/ui/tktile/tkcanvas.py b/pysollib/ui/tktile/tkcanvas.py index 5cd85b6f..9b572baf 100644 --- a/pysollib/ui/tktile/tkcanvas.py +++ b/pysollib/ui/tktile/tkcanvas.py @@ -59,7 +59,10 @@ class MfxCanvasImage(ImageItem2): group = kwargs['group'] del kwargs['group'] if 'image' in kwargs: - self._image = kwargs['image'] + im = kwargs['image'] + from pysollib.ui.tktile.tkutil import get_image_tk + im = get_image_tk(im) + kwargs['image'] = self._image = im ImageItem2.__init__(self, canvas, x, y, **kwargs) if group: self.addtag(group) diff --git a/pysollib/ui/tktile/tkutil.py b/pysollib/ui/tktile/tkutil.py index 0319e6d3..97ba4eea 100644 --- a/pysollib/ui/tktile/tkutil.py +++ b/pysollib/ui/tktile/tkutil.py @@ -42,6 +42,15 @@ def wm_deiconify(window): window.wm_deiconify() +def get_image_tk(self): + try: + return self.getTk() + except Exception: + if isinstance(self, ImageTk.PhotoImage): + return self + return ImageTk.PhotoImage(self) + + def wm_map(window, maximized=0): if window.wm_state() != "iconic": if maximized and WIN_SYSTEM == "win32": @@ -249,13 +258,21 @@ if Image: def __init__(self, file=None, image=None, pil_image_orig=None): if file: image = Image.open(file).convert('RGBA') - ImageTk.PhotoImage.__init__(self, image) + if isinstance(image, type(self)): + assert 0 + image = image._pil_image + print(type(image)) + # if not isinstance(image, ImageTk.PhotoImage): + ImageTk.PhotoImage.__init__(self, image=image) self._pil_image = image if pil_image_orig: self._pil_image_orig = pil_image_orig else: self._pil_image_orig = image + def getTk(self): + return ImageTk.PhotoImage(self._pil_image) + def subsample(self, r): im = self._pil_image w, h = im.size @@ -341,9 +358,16 @@ def fillImage(image, fill, outline=None): def createImage(width, height, fill, outline=None): - image = tkinter.PhotoImage(width=width, height=height) - assert image.width() == width - assert image.height() == height + image = ImageTk.PhotoImage(size=(width, height), image='RGBA') + image = Image.new('RGBA', (width, height), fill) + # image = tkinter.PhotoImage(width=width, height=height) + assert image.width == width + assert image.height == height + from PIL import ImageDraw + idraw = ImageDraw.Draw(image) + idraw.rectangle((0, 0, width, height), fill=fill) + # return ImageTk.PhotoImage(image=image) + return image image.blank() fillImage(image, fill, outline) return image