Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to set a timer #728

Closed
TaucherLoong opened this issue Jun 16, 2022 · 10 comments
Closed

how to set a timer #728

TaucherLoong opened this issue Jun 16, 2022 · 10 comments

Comments

@TaucherLoong
Copy link

I need to set a timer. Repeatedly a function checks whether data is updated, if yes update the gui face ? In flexx, does it has this functionality?

@TaucherLoong
Copy link
Author

[I 13:56:06 flexx.app] Starting Flexx event loop.
[I 13:56:08 flexx.app] New session ScrollExample ky0OsXaBn39pI0US7jcJ0ful
JS: C: foo changed:
JS: from 0 to 0
JS: C: foo changed:
JS: from 0 to 5
JS: from 5 to 0
continue
Exception in thread Thread-3:
Traceback (most recent call last):
File "d:\pythonversion38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "d:\pythonversion38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "d:/yolov5_deepsort/Yolov5_DeepSort_Pytorch/taucher_seminar/flexCart/scrollable.py", line 68, in check
event_var.set_foo(n)
File "D:.virtualenvs\smartcart\lib\site-packages\flexx\app_component2.py", line 41, in flx_proxy_action
self._proxy_action(flx_name, *args)
File "D:.virtualenvs\smartcart\lib\site-packages\flexx\app_component2.py", line 499, in _proxy_action
self._session.send_command('INVOKE', self._id, name, args)
File "D:.virtualenvs\smartcart\lib\site-packages\flexx\app_session.py", line 528, in send_command
self._ws.write_command(command)
File "D:.virtualenvs\smartcart\lib\site-packages\flexx\app_tornadoserver.py", line 614, in write_command
self.write_message(bb, binary=True)
File "D:.virtualenvs\smartcart\lib\site-packages\tornado\websocket.py", line 340, in write_message
return self.ws_connection.write_message(message, binary=binary)
File "D:.virtualenvs\smartcart\lib\site-packages\tornado\websocket.py", line 1096, in write_message
fut = self._write_frame(True, opcode, message, flags=flags)
File "D:.virtualenvs\smartcart\lib\site-packages\tornado\websocket.py", line 1073, in _write_frame
return self.stream.write(frame)
File "D:.virtualenvs\smartcart\lib\site-packages\tornado\iostream.py", line 540, in write
future = Future() # type: Future[None]
File "d:\pythonversion38\lib\asyncio\events.py", line 639, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-3'.
JS: C: foo changed:
JS: from 0 to 1

@TaucherLoong
Copy link
Author

my code here:

class ScrollExample(flx.Widget):

CSS = """
.flx-ScrollExample {
    overflow-y: scroll;  // scroll or auto
}
"""
foo = event.IntProp(0, settable=True)
def init(self):
    # self.c=Test()
    with flx.Widget():
        with flx.VBox(flex=1) as self.VB:
            for i in range(100):
                flx.Button(text="button " + str(i))

@flx.reaction('VB.pointer_click')
def set_value(self,*e):
    self.set_foo(3)
    print('clicked')

@event.reaction('foo')
def react_to_foo_c(self, *events):
    print('C: foo changed:')
    for ev in events:
        print('    from %i to %i' % (ev.old_value, ev.new_value))
        with self.VB:
            flx.Button(text='button_add')

from threading import Thread
def check(event_var):
n=0
print('Get thread started')
while True:
event_var.set_foo(n)
time.sleep(3)
print('continue')
n+=1

if name == 'main':

m = flx.launch(ScrollExample)
thread=Thread(target=check,args=(m,),daemon=True)
thread.start()
m.set_foo(5)
flx.run()

@TaucherLoong
Copy link
Author

I have stranded with the issue for days, I am some new for flexx, hope somebodies can help solve it

@Konubinix
Copy link
Contributor

I recommend you send a file instead of putting the code in a comment.

In its current form, I think most people won't take the time to try to clean the code and run it.
image

At least I won't :-P

@TaucherLoong
Copy link
Author

scrollable.py.txt

@TaucherLoong
Copy link
Author

Thank you for your response, I attached the code file above.

@TaucherLoong
Copy link
Author

I recommend you send a file instead of putting the code in a comment.

In its current form, I think most people won't take the time to try to clean the code and run it. image

At least I won't :-P

hi, konubinx:
I am just a new one for flexx. I have attached the program above, you may help me solve my issue. I would appreciate for it
Thks a lot

@Konubinix
Copy link
Contributor

I think you are doing it wrong. I would use threading to do this, but
rather use the asyncio call_later function.

I think you can manage to do something using threads, but it comes with
a lot of issues to deal with concurrency.

Here is what I would do:

  1. have a PyComponent to deal with the server side of stuff
  2. use call_later in the PyComponent to update the UI

There are alternative designs, like

  1. keeping only one Widget and use the async loop of the browser to call later
  2. insist on using threads

But I changed the code to make it more like I feel is the best way to go.

scrollable.py.txt

JS: C: foo changed:
JS:     from 0 to 0
JS: C: foo changed:
JS:     from 0 to 1
JS:     from 1 to 8
JS: C: foo changed:
JS:     from 8 to 9
JS: C: foo changed:
JS:     from 9 to 10
JS: clicked
JS: C: foo changed:
JS:     from 10 to 3
JS: C: foo changed:
JS:     from 3 to 4
JS: C: foo changed:
JS:     from 4 to 5
JS: C: foo changed:
JS:     from 5 to 6

Hope that helps and gets out started.

@TaucherLoong
Copy link
Author

konubinix:
Thank you very much. that is so helpful.

@almarklein
Copy link
Member

I agree with @Konubinix's answer: asyncio call_later is the way to go on the Python side.

For completeness, if you're only interested in a timer on the JS side, use window.setTimeout or window.setInterval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants