Repository: qtile/qtile

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?


Discussion:
I don't think we have something like that, but it should be pretty straight forward to get what you want, especially if you update the widget as part of the same operation that changes the layout itself.

There are probably a few ways to do this. The most elaborate would be  making a `GenPollImage` widget. The simplest would be to add a method to the `Image` widget so that it can be updated with a new image, and then using that method as part of whatever operation you are using to change the layout. This latter approach would require you to use something like a Qtile keybind to change layout, so wouldn't work so well if you're changing layouts from scripts or something.
@m-col I thought to create GenPollImage that updates from the command by getting new image path. Should it have something more?
If all you want to do is provide the widget with a new image from a command, then I think you best bet would be to add a command to the `Image` widget (e.g. `Image.cmd_update`). A `GenPollImage` implies that it is polling, so if you don't need the polling, then the update command is the way to go.
@m-col tried to write something. The hardest part is to script in config.py, because there is no documentation or I don't know how to find :D So, the question is how to declare that `cmd_update` that I could call it as `update`? In config.py I can reach only like:
```
@lazy.function
def z_next_keyboard(qtile):
    call(['xkb-switch', '-n'])
    filenames = {
        'us': '~/.config/qtile/flags/en.png',
        'lt(sgs)': '~/.config/qtile/flags/sgs.png',
        'ru(phonetic)': '~/.config/qtile/flags/ru.png',
        }
    keyboard = check_output(
            ['xkb-switch', '-p'],
            shell=True).decode('utf-8').replace('\n', '')
    qtile.widgets_map.get('image').cmd_update(filenames[keyboard])
```
and key 
```
    EzKey("A-<space>", z_next_keyboard, desc='switch keyboard layout'),
```
in widget/image.py I have simple code:
```
    def cmd_update(self, filename):
        self.filename = filename
        self._update_image()
        self.draw()
```

Resolving patch:
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 __init__(self, length=bar.CALCULATED, **config):
 
     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 @@ def calculate_length(self):
             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()


Test patch: