
When clock call a method it passes in the a time value. You don’t need to use this value, but you must have the right number of parameters.
Change:
def launch_cef_browser(self):
to:
def launch_cef_browser(self, dt):
When my code runs, the trigger_browser() function is called which, in turn, invokes launch_cef_browser(). This is the error that I get:
What am I missing here?
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/628ad0b2-baf3-4a54-b135-0cc21492ad3dn%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f546847.1c69fb81.60827.bca5SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
Here is a simple example using the threading module to create a background thread. You can start multiple background threads by pressing the ‘Start Background Thread’ button more that once. The Test button is the foreground task, you can see the UI remains interactive.
In addition to the python docs, there are examples of the threading module: https://pymotw.com/3/threading/index.html
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
import threading
import time
kv = """
BoxLayout:
orientation: 'vertical'
Label:
text: 'Threading Test'
Button:
text: 'Test Button'
on_release: print('Test Button Pressed')
ThreadButton:
text: 'Start Background Thread'
on_release: self.start_thread()
"""
class ThreadButton(Button):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.t = None
def start_thread(self):
print('Starting background thread')
self.t = threading.Thread(target=self.print_and_sleep)
self.t.start()
@staticmethod
def print_and_sleep(): # static methods do not use the self object
i = 0
while i < 20:
print(f'Useless background thread printing a count {i}')
i += 1
time.sleep(.5)
print('Background Thread Completed')
class ThreadTestApp(App):
def build(self):
return Builder.load_string(kv)
ThreadTestApp().run()
Thanks in advance.
What am I missing here?
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/628ad0b2-baf3-4a54-b135-0cc21492ad3dn%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f546847.1c69fb81.60827.bca5SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAH1Ve%3DSm2-5GjJx_o4Cxq4gCtepvBw36h%3Dq0frT5Dyyw%3DKeqkw%40mail.gmail.com.
which makes me wonder that the clock.schedule_once() statement is not launching the method in a different thread.
Thanks in advance.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/628ad0b2-baf3-4a54-b135-0cc21492ad3dn%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.


In threading, you want the thread to complete. Join alone will not do what you want. Join waits for the thread to complete. Imagine you had spawned a thread to do a task, and you wanted to wait until it was complete… then it would join the main thread.
I’m not familiar with the python cef browser, but looking at your code below I have a suggestion.
My expectation is that in the launch_cef_browser() method, the browser enters the MessageLoop(), and never returns, so Shutdown does not get called.
Use the screen event on_leave to shutdown the browser. I expect this would cause the MessageLoop to return, and the thread to complete.
def on_leave(self, *args):
cef.Shutdown()
Taking a quick look at the PythonCEF documents, perhaps you should call QuitMessageLoop()… you want to get the thread to complete.
From: Shoumik Das
Sent: Tuesday, September 8, 2020 3:47 AM
To: Kivy users support
Subject: Re: [kivy-users] Clock Schedule Once Error
Thanks, Elliot. I managed to trigger the cefpython browser in a separate thread as shown below. But I noticed that the thread does not exit on its own and continues to run in the background although it does not stop the execution of the Kivy app. As a result, if I click on the same button again, a new browser window does not open. Is there a way to close the current browser thread as soon as the associated window is closed?
Code
class SivaCEFBrowser(Screen):
def back_to_login(self):
App.get_running_app().root.current='login_screen'
App.get_running_app().root.transition.direction='right'
def go_to_verify(self):
App.get_running_app().root.current='verify_screen'
App.get_running_app().root.transition.direction='left'
def launch_cef_browser(self): # Use an additional parameter, 'dt', if invoked using Clock.schedule_once().
sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error.
cef.Initialize()
cef.CreateBrowserSync(url="https://www.google.com/", window_title="Hello World!")
cef.MessageLoop()
cef.Shutdown()
def trigger_browser(self):
#Clock.schedule_once(self.launch_cef_browser) # Starts in the same thread and pauses execution of Kivy code.
self.t=threading.Thread(target=self.launch_cef_browser)
self.t.start()
Screenshots
Browser is invoked successfully in a separate thread and the app proceeds to the next screen.
Now, when I close the browser window, its thread still seems to be running. This does not let me invoke a new browser window again. I want to kill the browser's thread once the window is closed. I was going trhough the article that you shared and came across a statement called t.join() for terminating a session but I am not sure how to use it. Can you please advise?
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/3fd55f9f-19c0-4936-aa29-aa8e9ab313can%40googlegroups.com.
Reading the PythonCEF docs, to looks like calling QuitMessageLoop() is the right way to go.
https://github.com/cztomczak/cefpython/blob/master/api/cefpython.md#quitmessageloop
class SivaCEFBrowser(Screen):
def on_leave(self, *args):
cef.QuitMessageLoop()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f578842.1c69fb81.b4a69.67f3SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
I’ve not done any work on Android – perhaps Robert has an idea.
One thought:
Code
Now, when I close the browser window, its thread still seems to be running. This does not let me invoke a new browser window again. I want to kill the browser's thread once the window is closed. I was going trhough the article that you shared and came across a statement called t.join() for terminating a session but I am not sure how to use it. Can you please advise?
Thanks as always for your help and advice.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/051ee756-0bbe-45f2-bb1c-3a3f751faa5an%40googlegroups.com.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.clock import Clock
from jnius import autoclass
from android import mActivity
from android.runnable import run_on_ui_thread
'''
Reference:
https://groups.google.com/forum/?oldui=1#!topic/kivy-users/9sH0y3KxCuU
buildozer.spec:
android.permissions = INTERNET
'''
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
# See https://github.com/kivy/python-for-android/issues/1908
@run_on_ui_thread
def create_webview(*args):
webview = WebView(mActivity)
webview.getSettings().setJavaScriptEnabled(True)
wvc = WebViewClient()
webview.setWebViewClient(wvc)
mActivity.setContentView(webview)
webview.loadUrl('https://www.google.com')
class Wv(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(create_webview, 0)
class WvDemo(App):
def build(self):
return Wv()
WvDemo().run()
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/f3599493-7611-43d9-a6fa-bb0276fe0b3ao%40googlegroups.com.