You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have tried various methods, and all of them have various issues.
Updating the main thread's form in a child thread, obviously, it failed.
from delphifmx import *
import threading
from pynput.mouse import Listener
import time
class GetMousePosition(threading.Thread):
def __init__(self, form):
super().__init__()
self.form = form
# mouseListener
def on_move(self, x, y):
print(f"Mouse is at: X={x}, Y={y}")
self.position = (x, y)
self.form.update_mouse_position(x, y)
def run(self):
with Listener(on_move=self.on_move) as listener:
listener.join()
class MyForm(Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.prev_position = None
self.mouse_thread = None
self.button = Button(self)
self.button.Text = 'Start Background Task'
self.button.OnClick = self.onStartButtonClick
self.label = Label(self)
self.label.Text = 'Click the button to start the background task'
self.button.Parent = self
self.button.Align = "Top"
self.label.Parent = self
self.label.Align = "Top"
def update_mouse_position(self, x, y):
self.label.Text = f'Mouse Position: ({x}, {y})'
def onStartButtonClick(self, sender):
self.mouse_thread = GetMousePosition(self)
self.mouse_thread.start()
if __name__ == '__main__':
Application.Initialize()
Application.Title = "Hello DelphiFMX"
app = MyForm(Application)
Application.MainForm = app
app.Show()
Application.Run()
app.Destroy()
2.Return data in the child thread, and the main thread uses onTimer to periodically read and update the main thread's form. It works. However, dragging the main interface causes stuttering, and mouse movement seems to be unsmooth as well.
from delphifmx import *
import threading
from pynput.mouse import Listener
import time
class GetMousePosition(threading.Thread):
def __init__(self):
super().__init__()
self.position = ()
self.position_history = []
def on_move(self, x, y):
print(f"Mouse is at: X={x}, Y={y}")
self.position = (x, y)
def run(self):
with Listener(on_move=self.on_move) as listener:
listener.join()
class MyForm(Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.prev_position = None
self.mouse_thread = None
self.button = Button(self)
self.button.Text = 'Start Background Task'
self.button.OnClick = self.onStartButtonClick
self.label = Label(self)
self.label.Text = 'Click the button to start the background task'
self.button.Parent = self
self.button.Align = "Top"
self.label.Parent = self
self.label.Align = "Top"
self.timer = Timer(self)
self.timer.Interval = 1
self.timer.OnTimer = self.update_ui
def update_ui(self, sender):
if self.mouse_thread:
result = self.mouse_thread.position
if result != self.prev_position:
print("result:" + str(result))
self.prev_position = result
self.label.Text = f"Mouse Position: " + str(result)
def onStartButtonClick(self, sender):
self.mouse_thread = GetMousePosition()
self.mouse_thread.start()
if __name__ == '__main__':
Application.Initialize()
Application.Title = "Hello DelphiFMX"
app = MyForm(Application)
Application.MainForm = app
app.Show()
Application.Run()
app.Destroy()
Using multiprocessing, data is stored in a queue within the child process, and the main thread uses onTimer to periodically read from and update the main thread's form. It also works. However, if the mouse keeps sliding, it will lead to a lot of data in the queue, and when the mouse stops, the form is still queuing to read data from the queue. It looks like the display of the form is somewhat lagging.
I didn't understand your example very well. Why are you using a thread to listen for mouse movements? There's an event in FMX forms for that purpose. Look for a "OnMouseMove" event.
If you still want to synch GUI operations: use a queue.Queue instance. In your thread you can queue some data and also put an event in the main thread to check the queue periodically.
I have tried various methods, and all of them have various issues.
2.Return data in the child thread, and the main thread uses onTimer to periodically read and update the main thread's form. It works. However, dragging the main interface causes stuttering, and mouse movement seems to be unsmooth as well.
What is the most correct way to use multithreading or multiprocessing in DelphiFMX4Python? Please let me know, thank you.
The text was updated successfully, but these errors were encountered: