Skip to content

Commit

Permalink
bpo-25451: Add transparency methods to tkinter.PhotoImage. (pythonGH-…
Browse files Browse the repository at this point in the history
  • Loading branch information
ZackerySpytz authored and serhiy-storchaka committed Apr 5, 2019
1 parent f66e336 commit 50866e9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ Added method :meth:`~tkinter.Canvas.moveto`
in the :class:`tkinter.Canvas` class.
(Contributed by Juliette Monsel in :issue:`23831`.)

The :class:`tkinter.PhotoImage` class now has
:meth:`~tkinter.PhotoImage.transparency_get` and
:meth:`~tkinter.PhotoImage.transparency_set` methods. (Contributed by
Zackery Spytz in :issue:`25451`.)

time
----

Expand Down
9 changes: 9 additions & 0 deletions Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4124,6 +4124,15 @@ def write(self, filename, format=None, from_coords=None):
args = args + ('-from',) + tuple(from_coords)
self.tk.call(args)

def transparency_get(self, x, y):
"""Return True if the pixel at x,y is transparent."""
return self.tk.getboolean(self.tk.call(
self.name, 'transparency', 'get', x, y))

def transparency_set(self, x, y, boolean):
"""Set the transparency of the pixel at x,y."""
self.tk.call(self.name, 'transparency', 'set', x, y, boolean)


class BitmapImage(Image):
"""Widget which can display images in XBM format."""
Expand Down
9 changes: 9 additions & 0 deletions Lib/tkinter/test/test_tkinter/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,15 @@ def test_write(self):
self.assertEqual(image3.get(0, 0), image.get(4, 6))
self.assertEqual(image3.get(1, 2), image.get(5, 8))

def test_transparency(self):
image = self.create()
self.assertEqual(image.transparency_get(0, 0), True)
self.assertEqual(image.transparency_get(4, 6), False)
image.transparency_set(4, 6, True)
self.assertEqual(image.transparency_get(4, 6), True)
image.transparency_set(4, 6, False)
self.assertEqual(image.transparency_get(4, 6), False)


tests_gui = (MiscTest, BitmapImageTest, PhotoImageTest,)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add transparency methods to :class:`tkinter.PhotoImage`. Patch by Zackery
Spytz.

0 comments on commit 50866e9

Please sign in to comment.