How to compose widgets correctly in python kivy? - python

I would like to make it so that I can see the progress of the execution of the loop, but the ProgressBar only exits after the execution of this loop. How can I fix this?
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.progressbar import ProgressBar
class MyApp(App):
def build(self):
self.wid = FloatLayout()
self.prog_bar = ProgressBar(max=99999, pos = [0, -150])
self.wid.add_widget(self.prog_bar)
self.prog_bar.value = 0
for i in range(0, 99999):
self.prog_bar.value = i
print(i/99999)
return self.wid
if __name__ == '__main__':
MyApp().run()

Kivy uses the main thread of your App to update its widgets. You are running your loop on the main thread, so Kivy cannot update the ProgressBar until that loop completes. The fix is to do the loop in another thread:
class MyApp(App):
def build(self):
self.wid = FloatLayout()
self.prog_bar = ProgressBar(max=99999, pos=[0, -150])
self.wid.add_widget(self.prog_bar)
self.prog_bar.value = 0
threading.Thread(target=self.do_progress).start()
return self.wid
def do_progress(self):
for i in range(0, 99999):
self.prog_bar.value = i
print(i / 99999)

This is not about composition, but rather about multitasking, the thing is, if you do a locking loop like this, kivy can’t update anything until the loop is done (because nothing else happens while the function runs, no update of the UI or processing of any event). So what you want to do is allow the loop to run "concurrently" to the kivy event loop.
There are multiple ways to do this kind of things, they are all more or less suited to different situations.
kivy Clock allows scheduling a function to run later (schedule_once), possibly multiple times (schedule_interval), so you could have a more atomic function, that just increments the value of the progress bar once (not in a loop), and call this function using schedule interval. The issue is if you want to do actual work in the function, that could take significant enough time for the UI to be visibly blocked between increment of the progressbar value.
If that’s your case, because the function you are trying to run is slow, then you might want to run the function in a Thread instead, so it doesn’t block the kivy UI. You’ll be able to update the value of your progress bar from the the thread, if necessary, using the Clock to schedule a function doing just that (you need to be careful about not doing things that update the OpenGL context from a thread, as OpenGL doesn’t specify the behavior in such situation, so some graphic cards/drivers can be fine with it, but others will crash your program, so anything updating the UI from a thread must use the clock, there is a mainthread decorator provided in the clock module for that).
In some situation, you can (and thus want) avoid doing the long work beforehand, and find a way to do the minimal amount of work each time needed, and go on with the work, there are some abstractions of that idea, like RecycleView, that avoids creating widgets altogether, when you want a very long list of items to display, just creating the data beforehand, and letting it handle the creation/update of the widgets as needed. Of course, this might not apply to your situation, but it’s worth mentioning in case it is.
So in short: don’t do such loops, or do them in threads, think really hard if you actually need such a loop, and use clock events to update your UI when using threads.

Related

How to start and stop a while-loop with an ipywidget interactive Checkbox?

i try to start and stop a while-loop with a simple checkbox. But i don't know how i am able to update the checkbox-state in the while loop.
Below you can see what i have, but when i uncheck the box, the while loop won't recognize it.
I searched through the interactive functions but have not found any function to get the updated state of the checkbox.
Hope you guys can help me out :)
import ipywidgets
import time
def f(x):
while x==True:
print("!")
time.sleep(1)
c=ipywidgets.interactive(f,x=False)
c
If you create the checkbox explicitly with:
checkbox = widgets.Checkbox(description='click me')
... you can get the current value with checkbox.value.
Annoyingly, that's not enough to just allow your checkbox to halt the execution of a while loop. Once your while loop has started, it occupies the Python interpreter. There is therefore no space for the checkbox changes to be interpreted until the while loop finishes.
Since this is fundamentally a concurrency problem (you want to react to changes in the checkbox while also running a computation), you need to use Python's concurrency primitives. You can, for instance, achieve this with coroutines:
import ipywidgets as widgets
import asyncio
class HaltableExecutor:
def __init__(self, checkbox):
self._checkbox = checkbox
async def my_code(self):
# This is your user code
while True:
if self._checkbox.value:
print('running') # put your code here
await asyncio.sleep(0.1) # use this to temporarily give up control of the event loop to allow scheduling checkbox changes
def start(self):
print('starting')
asyncio.ensure_future(self.my_code())
Then, create and display your checkbox:
c = widgets.Checkbox(description='click me')
c
... and start your executor:
exe = HaltableExecutor(c)
exe.start()
The while loop will now run. At every iteration, we pause to give up control of the event loop with asyncio.sleep. The amount we sleep by is irrelevant, the point is to give control back. This gives the main thread an opportunity to deal with outstanding messages like checkbox changes. At the next iteration of the while loop, self._checkbox.value is checked again and so on.

Runtime progress status (like statusbar) in python

I'm trying to create simple progress status label for my Pyqt5 Python code and update it after every iteration of a loop in which a function does a bunch of stuff. The label I want to be updated is "status_label_counter". The below code only shows the part for creating labels and the exact place where I want to use the functionality I've mentioned.
#initialisation code, etc...
self.status_label_counter = QLabel()
self.status_label_from = QLabel(' from: ')
self.status_label_total = QLabel()
status_hbox = QHBoxLayout()
status_hbox.addStretch()
status_hbox.addWidget(self.status_label_counter)
status_hbox.addWidget(self.status_label_from)
status_hbox.addWidget(self.status_label_total)
status_hbox.addStretch()
#bunch of other code...
def create_ics(self):
counter = 0
self.status_label_total.setText(str(len(self.groups)))
for group in self.groups:
#does a bunch of stuff inside
group_manager.create_calendar_for(self.rows, group, self.term)
counter += 1
#for console output
print('iteration: ', counter)
#trying to update status counter
self.status_label_counter.setText(str(counter))
The problem is that I only see the update of both labels when the loop is done with the nested function. When I click a button that calls for "create_ics" function window becomes inactive for about 5 seconds, I see logs on console with count of iterations, but nothing happens in view.
The view (Qt) is locked in your main thread and never gets its chance to process its event loop and thus redraw itself. If you really want to do it this way, call:
self.status_label_counter.repaint()
After you set the text (and if you have some complex layout measuring call QApplication.processEvents() instead).
However, much better option would be to run your create_ics() function in a separate thread leaving your main thread to deal with the view and Qt's event processing. You can do it either through standard Python's threading module, or using Qt's own QThread: https://nikolak.com/pyqt-threading-tutorial/ .

Memory leak when embedding and updating a matplotlib graph in a PyQt GUI

I am trying to embed a matplotlib graph that updates every second into a PyQt GUI main window.
In my program I call an update function every second using threading.Timer via the timer function shown below. I have a problem: my program grows bigger every second - at a rate of about 1k every 4 seconds. My initial thoughts are that the append function (that returns a new array in update_figure) does not delete the old array? Is it possible this is the cause of my problem?
def update_figure(self):
self.yAxis = np.append(self.yAxis, (getCO22()))
self.xAxis = np.append(self.xAxis, self.i)
# print(self.xAxis)
if len(self.yAxis) > 10:
self.yAxis = np.delete(self.yAxis, 0)
if len(self.xAxis) > 10:
self.xAxis = np.delete(self.xAxis, 0)
self.axes.plot(self.xAxis, self.yAxis, scaley=False)
self.axes.grid(True)
self.i = self.i + 1
self.draw()
This is my timer function - this is triggered by the click of a button in my PyQt GUI and then calls itself as you can see:
def timer(self):
getCH4()
getCO2()
getConnectedDevices()
self.dc.update_figure()
t = threading.Timer(1.0, self.timer)
t.start()
EDIT: I cant post my entire code because it requires a lot of .dll includes. So i'll try to explain what this program does.
In my GUI I want to show the my CO2 value over time. My get_co22 function just returns a float value and I'm 100% sure this works fine. With my timer, shown above, I want to keep append a value to a matplotlib graph - the Axes object is available to me as self.axes. I try to plot the last 10 values of the data.
EDIT 2: After some discussion in chat, I tried putting the call to update_figure() in a while loop and using just one thread to call it and was able to make this minimal example http://pastebin.com/RXya6Zah. This changed the structure of the code to call update_figure() to the following:
def task(self):
while True:
ui.dc.update_figure()
time.sleep(1.0)
def timer(self):
t = Timer(1.0, self.task())
t.start()
but now the program crashes after 5 iterations or so.
The problem is definitely not with how you are appending to your numpy array, or truncating it.
The problem here is with your threading model. Integrating calculation loops with a GUI control loop is difficult.
Fundamentally, you need your GUI threading to have control of when your update code is called (spawning a new thread to handle it if necessary) - so that
your code does not block the GUI updating,
the GUI updating does not block your code executing and
you don't spawn loads of threads holding multiple copies of objects (which might be where your memory leak comes from).
In this case, as your main window is controlled by PyQt4, you want to use a QTimer (see a simple example here)
So - alter your timer code to
def task(self):
getCH4()
getCO2()
getConnectedDevices()
self.dc.update_figure()
def timer(self):
self.t = QtCore.QTimer()
self.t.timeout.connect(self.task)
self.t.start(1000)
and this should work. Keeping the reference to the QTimer is essential - hence self.t = QtCore.QTimer() rather than t = QtCore.QTimer(), otherwise the QTimer object will be garbage collected.
Note:
This is a summary of a long thread in chat clarifying the issue and working through several possible solutions. In particular - the OP managed to mock up a simpler runnable example here: http://pastebin.com/RXya6Zah
and the fixed version of the full runnable example is here: http://pastebin.com/gv7Cmapr
The relevant code and explanation is above, but the links might help anyone who wants to replicate / solve the issue. Note that they require PyQt4 to be installed
if you are creating a new figure for every time this is quite common.
matplotlib do not free the figures you create, unless you ask it, somethink like:
pylab.close()
see How can I release memory after creating matplotlib figures

Python Kivy: Properly start a background process that updates GUI elements

I have a Python script that performs some intensive processing of user's files and can take some time. I've build a user interface to it using Kivy, that allows the user to select the file, processing mode and shows them some messages as the process goes on.
My problem is that when the main Kivy loop passes calls the underlying user interface, the window freezes.
From what I've understood, the proper way of resolving this is to create a separate process to which the script would be off-loaded and from which it would send the updates to the user interface.
However, I was not able to find an example of how to do this or any specification on how to send messages from a separate thread back into application.
Could someone please give an example of how to do this properly or point me to the documentation pertaining to the subject?
Update:
For the sake of keeping the program maintainable I would like to avoid calling the elements of loops of processor from the main thread and instead call one long process that comes back to updated elements of the GUI, such as the progress bar or a text field. It looks like those elements can be modified only from the main kivy thread. How do I gain access to them from the outside?
Use publisher/consumer model as described here. Here's an example from that link modified to use separate threads:
from kivy.app import App
from kivy.clock import Clock, _default_time as time # ok, no better way to use the same clock as kivy, hmm
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.uix.button import Button
from kivy.properties import ListProperty
from threading import Thread
from time import sleep
MAX_TIME = 1/60.
kv = '''
BoxLayout:
ScrollView:
GridLayout:
cols: 1
id: target
size_hint: 1, None
height: self.minimum_height
MyButton:
text: 'run'
<MyLabel#Label>:
size_hint_y: None
height: self.texture_size[1]
'''
class MyButton(Button):
def on_press(self, *args):
Thread(target=self.worker).start()
def worker(self):
sleep(5) # blocking operation
App.get_running_app().consommables.append("done")
class PubConApp(App):
consommables = ListProperty([])
def build(self):
Clock.schedule_interval(self.consume, 0)
return Builder.load_string(kv)
def consume(self, *args):
while self.consommables and time() < (Clock.get_time() + MAX_TIME):
item = self.consommables.pop(0) # i want the first one
label = Factory.MyLabel(text=item)
self.root.ids.target.add_widget(label)
if __name__ == '__main__':
PubConApp().run()
I think it's worth providing a 2022 update. Kivy apps can now be run via Python's builtin asyncio library and utilities. Previously, the problem was there was no way to return control to the main Kivy event loop when an async function call finished, hence you could not update the GUI. Now, Kivy runs in the same event loop as any other asyncio awaitables (relevant docs).
To run the app asynchronously, replace the YourAppClass().run() at the bottom of your main.py with this:
import asyncio
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(
YourAppClass().async_run()
)
loop.close()
And that's about it. With regards to the docs:
It is fully safe to interact with any kivy object from other
coroutines running within the same async event loop. This is because
they are all running from the same thread and the other coroutines are
only executed when Kivy is idling.
Similarly, the kivy callbacks may safely interact with objects from
other coroutines running in the same event loop. Normal single
threaded rules apply to both case.
If explicitly need to create a new thread, #Nykakin 's approach is what you want. I'd recommend using Queues to pass data between threads, instead, because they're simpler to implement and more robust, being specifically designed for this purpose. If you just want asynchronicity, async_run() is your best friend.
BE WARNED: While modifying a kivy property from another thread nominally works, there is every indication that this is not a thread safe operation. (Use a debugger and step through the append function in the background thread.) Altering a kivy property from another thread states that you should not modify a property in this way.

Update progress bar from sub-routine

I would like to update a progress bar that I have on a main window with the progress of a task that I am doing on another sub-routine, would it be possible??
To be as clear as possible, I would have 2 files:
In my Mainwindow.py I would have something like:
import Calculations
#some code
self.ui.progressBar
Calculations.longIteration("parameters")
Then I would have a separate file for the calculations: Calculations.py
def longIteration("parameters")
#some code for the loop
"here I would have a loop running"
"And I would like to update the progressBar in Mainwindow"
Is that possible?
Or it should be done on a different way?
Thanks.
The simplest of methods would be to simply pass the GUI object:
self.ui.progressBar
Calculations.longIteration("parameters", self.ui.progressBar)
and update progressBar on Calculations. This has two problems, though:
You're mixing GUI code with Calculations, who probably shouldn't know anything about it
if longIteration is a long running function, as its name implies, you're blocking your GUI main thread, which will make many GUI frameworks unhappy (and your application unresponsive).
Another solution is running longIteration in a thread, and pass a callback function that you use to update your progress bar:
import threading
def progress_callback():
#update progress bar here
threading.Thread(target=Calculations.longIteration, args=["parameters", progress_callback]).run()
then, inside longIteration, do:
def longIteration( parameters, progress_callback ):
#do your calculations
progress_callback() #call the callback to notify of progress
You can modify the progress_callback to take arguments if you need them, obviously

Categories