0

I am developing an interface with Kivy. I try to place a progress bar at the top right ora text which and a number which evolves every second. Kind of like the example here: Kivy custom shaders touch events. When I launch the application, the pogress bar is not visible.

Here is an example of what I'd like to be displayed.

main.py:

class Star:
    angle = 0
    distance = 0
    size = 0.1

    def __init__(self, sf, i):
        self.sf = sf
        self.j = 4 * i * sf.vsize
        self.reset()

    def reset(self):
        self.angle = 2 * math.pi * random()
        self.distance = 90 * random() + 10
        self.size = 0.05 * random() + 0.05

    def iterate(self):
        return range(self.j,
                     self.j + 4 * self.sf.vsize,
                     self.sf.vsize)

    def update(self, x0, y0):
        x = x0 + self.distance * math.cos(self.angle)
        y = y0 + self.distance * math.sin(self.angle)

        for i in self.iterate():
            self.sf.vertices[i:i + 3] = (x, y, self.size)
  • Post an example that doesn't include any unnecessary details (i.e. all of this drawing), and does include your attempt to display a progress bar. – inclement Sep 06 '20 at 11:47

1 Answers1

0

The easiest way to add a ProgressBar to your display is to use a Layout as the root of your display rather than using the Starfield as root. So, if you define your display using a kv string like this:

kv = '''
FloatLayout:
    Starfield:
        id: sf
        progress: pb
    ProgressBar:
        id: pb
        pos_hint: {'right':1.0, 'top':1.0}
        size_hint: (0.25, 0.1)
'''

Then you can use that in your build() method:

class StarfieldApp(App):
    def build(self):
        EventLoop.ensure_window()
        return Builder.load_string(kv)

    def on_start(self):
        Clock.schedule_interval(self.root.ids.sf.update_glsl, 60 ** -1)

Note that the Clock.schedule_interval() argument has changed (since Starfield is no longer the root).

Then you can simply add updates to the ProgressBar in your update_glsl() method. Something like this:

self.progress.value += 1

will demonstrate the concept, but is obviously not the complete solution.

John Anderson
  • 35,991
  • 4
  • 13
  • 36
  • It worked when I tried it. What happens when you try it? Do you get any errors? – John Anderson Sep 06 '20 at 16:29
  • I have a black screen with the progress bar, but the stars have disappeared –  Sep 06 '20 at 16:34
  • In fact, I just want to add a progress bar at the top right of the application, or a text and a second counter. –  Sep 06 '20 at 16:35
  • Check that your `Clock.schedule_interval()` call has been changed. – John Anderson Sep 06 '20 at 16:58
  • The problem is just with initialize –  Sep 06 '20 at 17:37
  • With the modifications in my answer, the `root` of your `App` is now the `FloatLayout` from the `kv` string. That is why `self.root.initialize()` is showing an error. It looks like you might be able to replace that with `self.root.ids.sf.initialize()`. – John Anderson Sep 06 '20 at 18:37
  • I have one last question, because you are too strong: Is it possible to have multiple windows with your solution? –  Sep 06 '20 at 19:45
  • Have a look at [this question](https://stackoverflow.com/questions/39359950/how-do-i-have-multiple-windows-in-kivy). – John Anderson Sep 06 '20 at 19:58
  • Thanks. Is it possible to change the color of the progress bar and the background? –  Sep 07 '20 at 12:54
  • Yes, but that should be posted as a separate question. – John Anderson Sep 07 '20 at 15:24