We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Widget for updatable image
I don't want to reinvent the wheel, so will check before.
I use GenPollText for my keyboard layout indicator but instead I want to see the flag (image). As I change layout the image should be changed. Do Qtile has such widget or proper way to do that?

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `libqtile/widget/image.py`
Content:
```
1 # Copyright (c) 2013 dequis
2 # Copyright (c) 2014 Sean Vig
3 # Copyright (c) 2014 Adi Sieker
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22 import os
23 
24 from libqtile import bar
25 from libqtile.images import Img
26 from libqtile.log_utils import logger
27 from libqtile.widget import base
28 
29 
30 class Image(base._Widget, base.MarginMixin):
31     """Display a PNG image on the bar"""
32     orientations = base.ORIENTATION_BOTH
33     defaults = [
34         ("scale", True, "Enable/Disable image scaling"),
35         ("rotate", 0.0, "rotate the image in degrees counter-clockwise"),
36         ("filename", None, "Image filename. Can contain '~'"),
37     ]
38 
39     def __init__(self, length=bar.CALCULATED, **config):
40         base._Widget.__init__(self, length, **config)
41         self.add_defaults(Image.defaults)
42         self.add_defaults(base.MarginMixin.defaults)
43 
44         # make the default 0 instead
45         self._variable_defaults["margin"] = 0
46 
47     def _configure(self, qtile, bar):
48         base._Widget._configure(self, qtile, bar)
49         self.img = None
50 
51         if not self.filename:
52             logger.warning("Image filename not set!")
53             return
54 
55         self.filename = os.path.expanduser(self.filename)
56 
57         if not os.path.exists(self.filename):
58             logger.warning("Image does not exist: {}".format(self.filename))
59             return
60 
61         img = Img.from_path(self.filename)
62         self.img = img
63         img.theta = self.rotate
64         if not self.scale:
65             return
66         if self.bar.horizontal:
67             new_height = self.bar.height - (self.margin_y * 2)
68             img.resize(height=new_height)
69         else:
70             new_width = self.bar.width - (self.margin_x * 2)
71             img.resize(width=new_width)
72 
73     def draw(self):
74         if self.img is None:
75             return
76 
77         self.drawer.clear(self.background or self.bar.background)
78         self.drawer.ctx.save()
79         self.drawer.ctx.translate(self.margin_x, self.margin_y)
80         self.drawer.ctx.set_source(self.img.pattern)
81         self.drawer.ctx.paint()
82         self.drawer.ctx.restore()
83 
84         if self.bar.horizontal:
85             self.drawer.draw(offsetx=self.offset, offsety=self.offsety, width=self.width)
86         else:
87             self.drawer.draw(offsety=self.offset, offsetx=self.offsetx, height=self.width)
88 
89     def calculate_length(self):
90         if self.img is None:
91             return 0
92 
93         if self.bar.horizontal:
94             return self.img.width + (self.margin_x * 2)
95         else:
96             return self.img.height + (self.margin_y * 2)
97
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/libqtile/widget/image.py b/libqtile/widget/image.py
--- a/libqtile/widget/image.py
+++ b/libqtile/widget/image.py
@@ -46,6 +46,9 @@
 
     def _configure(self, qtile, bar):
         base._Widget._configure(self, qtile, bar)
+        self._update_image()
+
+    def _update_image(self):
         self.img = None
 
         if not self.filename:
@@ -94,3 +97,13 @@
             return self.img.width + (self.margin_x * 2)
         else:
             return self.img.height + (self.margin_y * 2)
+
+    def cmd_update(self, filename):
+        old_length = self.calculate_length()
+        self.filename = filename
+        self._update_image()
+
+        if self.calculate_length() == old_length:
+            self.draw()
+        else:
+            self.bar.draw()

{"golden_diff": "diff --git a/libqtile/widget/image.py b/libqtile/widget/image.py\n--- a/libqtile/widget/image.py\n+++ b/libqtile/widget/image.py\n@@ -46,6 +46,9 @@\n \n     def _configure(self, qtile, bar):\n         base._Widget._configure(self, qtile, bar)\n+        self._update_image()\n+\n+    def _update_image(self):\n         self.img = None\n \n         if not self.filename:\n@@ -94,3 +97,13 @@\n             return self.img.width + (self.margin_x * 2)\n         else:\n             return self.img.height + (self.margin_y * 2)\n+\n+    def cmd_update(self, filename):\n+        old_length = self.calculate_length()\n+        self.filename = filename\n+        self._update_image()\n+\n+        if self.calculate_length() == old_length:\n+            self.draw()\n+        else:\n+            self.bar.draw()\n", "issue": "Widget for updatable image\nI don't want to reinvent the wheel, so will check before.\r\nI use GenPollText for my keyboard layout indicator but instead I want to see the flag (image). As I change layout the image should be changed. Do Qtile has such widget or proper way to do that?\n", "before_files": [{"content": "# Copyright (c) 2013 dequis\n# Copyright (c) 2014 Sean Vig\n# Copyright (c) 2014 Adi Sieker\n#\n# Permission is hereby granted, free of charge, to any person obtaining a copy\n# of this software and associated documentation files (the \"Software\"), to deal\n# in the Software without restriction, including without limitation the rights\n# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n# copies of the Software, and to permit persons to whom the Software is\n# furnished to do so, subject to the following conditions:\n#\n# The above copyright notice and this permission notice shall be included in\n# all copies or substantial portions of the Software.\n#\n# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n# SOFTWARE.\nimport os\n\nfrom libqtile import bar\nfrom libqtile.images import Img\nfrom libqtile.log_utils import logger\nfrom libqtile.widget import base\n\n\nclass Image(base._Widget, base.MarginMixin):\n    \"\"\"Display a PNG image on the bar\"\"\"\n    orientations = base.ORIENTATION_BOTH\n    defaults = [\n        (\"scale\", True, \"Enable/Disable image scaling\"),\n        (\"rotate\", 0.0, \"rotate the image in degrees counter-clockwise\"),\n        (\"filename\", None, \"Image filename. Can contain '~'\"),\n    ]\n\n    def __init__(self, length=bar.CALCULATED, **config):\n        base._Widget.__init__(self, length, **config)\n        self.add_defaults(Image.defaults)\n        self.add_defaults(base.MarginMixin.defaults)\n\n        # make the default 0 instead\n        self._variable_defaults[\"margin\"] = 0\n\n    def _configure(self, qtile, bar):\n        base._Widget._configure(self, qtile, bar)\n        self.img = None\n\n        if not self.filename:\n            logger.warning(\"Image filename not set!\")\n            return\n\n        self.filename = os.path.expanduser(self.filename)\n\n        if not os.path.exists(self.filename):\n            logger.warning(\"Image does not exist: {}\".format(self.filename))\n            return\n\n        img = Img.from_path(self.filename)\n        self.img = img\n        img.theta = self.rotate\n        if not self.scale:\n            return\n        if self.bar.horizontal:\n            new_height = self.bar.height - (self.margin_y * 2)\n            img.resize(height=new_height)\n        else:\n            new_width = self.bar.width - (self.margin_x * 2)\n            img.resize(width=new_width)\n\n    def draw(self):\n        if self.img is None:\n            return\n\n        self.drawer.clear(self.background or self.bar.background)\n        self.drawer.ctx.save()\n        self.drawer.ctx.translate(self.margin_x, self.margin_y)\n        self.drawer.ctx.set_source(self.img.pattern)\n        self.drawer.ctx.paint()\n        self.drawer.ctx.restore()\n\n        if self.bar.horizontal:\n            self.drawer.draw(offsetx=self.offset, offsety=self.offsety, width=self.width)\n        else:\n            self.drawer.draw(offsety=self.offset, offsetx=self.offsetx, height=self.width)\n\n    def calculate_length(self):\n        if self.img is None:\n            return 0\n\n        if self.bar.horizontal:\n            return self.img.width + (self.margin_x * 2)\n        else:\n            return self.img.height + (self.margin_y * 2)\n", "path": "libqtile/widget/image.py"}], "after_files": [{"content": "# Copyright (c) 2013 dequis\n# Copyright (c) 2014 Sean Vig\n# Copyright (c) 2014 Adi Sieker\n#\n# Permission is hereby granted, free of charge, to any person obtaining a copy\n# of this software and associated documentation files (the \"Software\"), to deal\n# in the Software without restriction, including without limitation the rights\n# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n# copies of the Software, and to permit persons to whom the Software is\n# furnished to do so, subject to the following conditions:\n#\n# The above copyright notice and this permission notice shall be included in\n# all copies or substantial portions of the Software.\n#\n# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n# SOFTWARE.\nimport os\n\nfrom libqtile import bar\nfrom libqtile.images import Img\nfrom libqtile.log_utils import logger\nfrom libqtile.widget import base\n\n\nclass Image(base._Widget, base.MarginMixin):\n    \"\"\"Display a PNG image on the bar\"\"\"\n    orientations = base.ORIENTATION_BOTH\n    defaults = [\n        (\"scale\", True, \"Enable/Disable image scaling\"),\n        (\"rotate\", 0.0, \"rotate the image in degrees counter-clockwise\"),\n        (\"filename\", None, \"Image filename. Can contain '~'\"),\n    ]\n\n    def __init__(self, length=bar.CALCULATED, **config):\n        base._Widget.__init__(self, length, **config)\n        self.add_defaults(Image.defaults)\n        self.add_defaults(base.MarginMixin.defaults)\n\n        # make the default 0 instead\n        self._variable_defaults[\"margin\"] = 0\n\n    def _configure(self, qtile, bar):\n        base._Widget._configure(self, qtile, bar)\n        self._update_image()\n\n    def _update_image(self):\n        self.img = None\n\n        if not self.filename:\n            logger.warning(\"Image filename not set!\")\n            return\n\n        self.filename = os.path.expanduser(self.filename)\n\n        if not os.path.exists(self.filename):\n            logger.warning(\"Image does not exist: {}\".format(self.filename))\n            return\n\n        img = Img.from_path(self.filename)\n        self.img = img\n        img.theta = self.rotate\n        if not self.scale:\n            return\n        if self.bar.horizontal:\n            new_height = self.bar.height - (self.margin_y * 2)\n            img.resize(height=new_height)\n        else:\n            new_width = self.bar.width - (self.margin_x * 2)\n            img.resize(width=new_width)\n\n    def draw(self):\n        if self.img is None:\n            return\n\n        self.drawer.clear(self.background or self.bar.background)\n        self.drawer.ctx.save()\n        self.drawer.ctx.translate(self.margin_x, self.margin_y)\n        self.drawer.ctx.set_source(self.img.pattern)\n        self.drawer.ctx.paint()\n        self.drawer.ctx.restore()\n\n        if self.bar.horizontal:\n            self.drawer.draw(offsetx=self.offset, offsety=self.offsety, width=self.width)\n        else:\n            self.drawer.draw(offsety=self.offset, offsetx=self.offsetx, height=self.width)\n\n    def calculate_length(self):\n        if self.img is None:\n            return 0\n\n        if self.bar.horizontal:\n            return self.img.width + (self.margin_x * 2)\n        else:\n            return self.img.height + (self.margin_y * 2)\n\n    def cmd_update(self, filename):\n        old_length = self.calculate_length()\n        self.filename = filename\n        self._update_image()\n\n        if self.calculate_length() == old_length:\n            self.draw()\n        else:\n            self.bar.draw()\n", "path": "libqtile/widget/image.py"}]}