]> granicus.if.org Git - python/commitdiff
bpo-25451: Add transparency methods to tkinter.PhotoImage. (GH-10406)
authorZackery Spytz <zspytz@gmail.com>
Fri, 5 Apr 2019 10:17:13 +0000 (04:17 -0600)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 5 Apr 2019 10:17:13 +0000 (13:17 +0300)
Doc/whatsnew/3.8.rst
Lib/tkinter/__init__.py
Lib/tkinter/test/test_tkinter/test_images.py
Misc/NEWS.d/next/Library/2018-11-07-23-44-25.bpo-25451.re_8db.rst [new file with mode: 0644]

index 411f2299b290a1c4ecda8121c1955b46d7ca2895..0fc4d774bcde2b2dd7ad702e2c3d0e1023ebd065 100644 (file)
@@ -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
 ----
 
index ae493ed3aaaf3f2517701dbbceef84d2de992c5e..57d5b2572822820753fc01d01fecbc2624d1fb94 100644 (file)
@@ -4124,6 +4124,15 @@ class PhotoImage(Image):
             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."""
index 85a8cd0495ba88a050bf4610cbe538811a284cf2..2805d35a1f5b1bffde880cd179572c043827e05b 100644 (file)
@@ -320,6 +320,15 @@ class PhotoImageTest(AbstractTkTest, unittest.TestCase):
         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,)
 
diff --git a/Misc/NEWS.d/next/Library/2018-11-07-23-44-25.bpo-25451.re_8db.rst b/Misc/NEWS.d/next/Library/2018-11-07-23-44-25.bpo-25451.re_8db.rst
new file mode 100644 (file)
index 0000000..e0a9ea0
--- /dev/null
@@ -0,0 +1,2 @@
+Add transparency methods to :class:`tkinter.PhotoImage`.  Patch by Zackery
+Spytz.