mirror of https://github.com/kivy/kivy.git
Page:
Working with Python threads inside a Kivy application
Pages
A draggable scrollbar using a slider
AdBuddiz Android advertisements integration for Kivy apps
Advanced Graphics: In Progress
An example of background Twisted server running on Android
Android Background Services
Android SDK NDK Information
Android SDK NDK Informations
Android native embedded browser
Android style menu app skeleton
Background Service using P4A android.service
Batch installer for windows(KivyInstaller)
Breaking changes in Kivy
Building Portable Package
Button(s) in settings panel
Buttons in Settings panel
Community Guidelines
Connecting Kivy with Anaconda (OSX)
Contextual Menus
Contextual menus
Control alpha of all the children
Create source distribution release on PyPI
Creating a Release APK
Data driven variables with kivy properties
Debugging widget sizes
Deep Linking with iOS and Android
Delayed Work using Clock
Drag and Drop Widgets
Dragable Widget
Draggable Scalable Button
Editable ComboBox
Editable Label
Embedding a Carousel inside a TabbedPanel
GestureBox
Home
Implementing Android Adaptive Icons
KEP001: Instantiate things other than widgets from kv
Kivy 2.0 api breaks
Kivy Blogs and Blog Posts
Kivy Python 2 Support Timeline
Kivy Technical FAQ
Kv language preprocessing
Linking ScreenManager to a different Widget
List of Kivy Projects
Markup Summary
Menu on long touch
Migration guide from legacy garden packages
Moving kivy.garden.xxx to kivy_garden.xxx and kivy.deps.xxx to kivy_deps.xxx
On touch current widget
On touch on current widget
Packaging Kivy apps written in Python 3, targeting Windows using Nuitka
Pyjnius Vibrator Example
Release Checklist
Release notes for 1.10.0
Release notes for 1.11.0
Sample Gestures
Scaler for Retina screen
Scollable Options in Settings panel
Scrollable Label
Setting Up Kivy with various popular IDE's
Setting up Pycharm on OSX (older versions)
Setting up garden with Mac Ports
Setting up kivy with various popular IDE
Simple slider with value in label
Snippet template
Snippets awaiting moderation
Snippets
Starting Kivy App and Service on bootup on Android
Styling a Spinner and SpinnerOption in KV
Talks and tutorials
Theming Kivy
Tiled Maps & Tile Based Movement
Tiling the background of a widget with an image, pixel perfect
Ubuntu Touch
Updating widget content from a items list
User Snippets
Using Asynchronous programming inside a Kivy application
Using Buildozer on windows 10 using WSL
Viewport with fixed resolution autofit to window
Windows RT
Working with Python threads inside a Kivy application
rand0m app
wiki_proposed
12
Working with Python threads inside a Kivy application
Armin Sebastian edited this page 2018-10-03 16:45:44 +03:00
Table of Contents
Summary
- author: dessant
- kivy: >= 1.8.0
OpenGL related operations (widget, canvas, property manipulation etc.) should be done only in the main thread. This example shows us how to use Clock
or the mainthread
decorator to perform these actions, and how to use threading events to stop hanging threads when the app is about to close.
Usage
python main.py
Files
main.py
import threading
import time
from kivy.app import App
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.animation import Animation
from kivy.clock import Clock, mainthread
from kivy.uix.gridlayout import GridLayout
Builder.load_string("""
<AnimWidget@Widget>:
canvas:
Color:
rgba: 0.7, 0.3, 0.9, 1
Rectangle:
pos: self.pos
size: self.size
size_hint: None, None
size: 400, 30
<RootWidget>:
cols: 1
canvas:
Color:
rgba: 0.9, 0.9, 0.9, 1
Rectangle:
pos: self.pos
size: self.size
anim_box: anim_box
but_1: but_1
lab_1: lab_1
lab_2: lab_2
Button:
id: but_1
font_size: 20
text: 'Start second thread'
on_press: root.start_second_thread(lab_2.text)
Label:
id: lab_1
font_size: 30
color: 0.6, 0.6, 0.6, 1
text_size: self.width, None
halign: 'center'
AnchorLayout:
id: anim_box
Label:
id: lab_2
font_size: 100
color: 0.8, 0, 0, 1
text: '3'
""")
class RootWidget(GridLayout):
stop = threading.Event()
def start_second_thread(self, l_text):
threading.Thread(target=self.second_thread, args=(l_text,)).start()
def second_thread(self, label_text):
# Remove a widget, update a widget property, create a new widget,
# add it and animate it in the main thread by scheduling a function
# call with Clock.
Clock.schedule_once(self.start_test, 0)
# Do some thread blocking operations.
time.sleep(5)
l_text = str(int(label_text) * 3000)
# Update a widget property in the main thread by decorating the
# called function with @mainthread.
self.update_label_text(l_text)
# Do some more blocking operations.
time.sleep(2)
# Remove some widgets and update some properties in the main thread
# by decorating the called function with @mainthread.
self.stop_test()
# Start a new thread with an infinite loop and stop the current one.
threading.Thread(target=self.infinite_loop).start()
def start_test(self, *args):
# Remove the button.
self.remove_widget(self.but_1)
# Update a widget property.
self.lab_1.text = ('The UI remains responsive while the '
'second thread is running.')
# Create and add a new widget.
anim_bar = Factory.AnimWidget()
self.anim_box.add_widget(anim_bar)
# Animate the added widget.
anim = Animation(opacity=0.3, width=100, duration=0.6)
anim += Animation(opacity=1, width=400, duration=0.8)
anim.repeat = True
anim.start(anim_bar)
@mainthread
def update_label_text(self, new_text):
self.lab_2.text = new_text
@mainthread
def stop_test(self):
self.lab_1.text = ('Second thread exited, a new thread has started. '
'Close the app to exit the new thread and stop '
'the main process.')
self.lab_2.text = str(int(self.lab_2.text) + 1)
self.remove_widget(self.anim_box)
def infinite_loop(self):
iteration = 0
while True:
if self.stop.is_set():
# Stop running this thread so the main Python process can exit.
return
iteration += 1
print('Infinite loop, iteration {}.'.format(iteration))
time.sleep(1)
class ThreadedApp(App):
def on_stop(self):
# The Kivy event loop is about to stop, set a stop signal;
# otherwise the app window will close, but the Python process will
# keep running until all secondary threads exit.
self.root.stop.set()
def build(self):
return RootWidget()
if __name__ == '__main__':
ThreadedApp().run()
##Comments
Section for user comments
Why must OpenGL operations be done in the main thread? Is it possible to have a main control/adapter thread that opens a thread for kivy and a thread for model operations?
The @mainthread is not necessarily the main thread but the thread that creates the kivy app to run. Despite this unfavorable limitation, a companion thread would suffice for other non-kivy event processing and interacts with kivy UI through @mainthread or clock scheduling.