Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 50866e9

Browse files
ZackerySpytzserhiy-storchaka
authored andcommitted
bpo-25451: Add transparency methods to tkinter.PhotoImage. (GH-10406)
1 parent f66e336 commit 50866e9

4 files changed

Lines changed: 25 additions & 0 deletions

File tree

Doc/whatsnew/3.8.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,11 @@ Added method :meth:`~tkinter.Canvas.moveto`
379379
in the :class:`tkinter.Canvas` class.
380380
(Contributed by Juliette Monsel in :issue:`23831`.)
381381

382+
The :class:`tkinter.PhotoImage` class now has
383+
:meth:`~tkinter.PhotoImage.transparency_get` and
384+
:meth:`~tkinter.PhotoImage.transparency_set` methods. (Contributed by
385+
Zackery Spytz in :issue:`25451`.)
386+
382387
time
383388
----
384389

Lib/tkinter/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4124,6 +4124,15 @@ def write(self, filename, format=None, from_coords=None):
41244124
args = args + ('-from',) + tuple(from_coords)
41254125
self.tk.call(args)
41264126

4127+
def transparency_get(self, x, y):
4128+
"""Return True if the pixel at x,y is transparent."""
4129+
return self.tk.getboolean(self.tk.call(
4130+
self.name, 'transparency', 'get', x, y))
4131+
4132+
def transparency_set(self, x, y, boolean):
4133+
"""Set the transparency of the pixel at x,y."""
4134+
self.tk.call(self.name, 'transparency', 'set', x, y, boolean)
4135+
41274136

41284137
class BitmapImage(Image):
41294138
"""Widget which can display images in XBM format."""

Lib/tkinter/test/test_tkinter/test_images.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,15 @@ def test_write(self):
320320
self.assertEqual(image3.get(0, 0), image.get(4, 6))
321321
self.assertEqual(image3.get(1, 2), image.get(5, 8))
322322

323+
def test_transparency(self):
324+
image = self.create()
325+
self.assertEqual(image.transparency_get(0, 0), True)
326+
self.assertEqual(image.transparency_get(4, 6), False)
327+
image.transparency_set(4, 6, True)
328+
self.assertEqual(image.transparency_get(4, 6), True)
329+
image.transparency_set(4, 6, False)
330+
self.assertEqual(image.transparency_get(4, 6), False)
331+
323332

324333
tests_gui = (MiscTest, BitmapImageTest, PhotoImageTest,)
325334

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add transparency methods to :class:`tkinter.PhotoImage`. Patch by Zackery
2+
Spytz.

0 commit comments

Comments
 (0)