-
The following code demonstrates a round button in the default Processing window but the button's 'quit' action throws an exception when using exit_sketch(). The issue seems to be due to shutting down a Processing window which is running a JavaFX Application Thread. Exception thrown is # Uses Imported mode for py5
# Error on 'quit': Not on FX application thread; currentThread = JavaFX Application Thread
import javafx
_wndW = 400
_wndH = 400
def myBtnAction(event):
# Works, but throws exception
exit_sketch()
def settings():
size(_wndW,_wndH,FX2D)
def setup():
canvas = get_surface().get_native()
window_title('JavaFX Round Button in Default Window')
canvas = get_surface().get_native()
root = canvas.getParent()
pane = javafx.scene.layout.Pane()
root.getChildren().add(pane)
quitBtn = javafx.scene.control.Button("Q")
quitBtn.setStyle("-fx-background-radius:5em;"+"-fx-min-width:30px;"+"-fx-min-height:30px;"+"-fx-max-width:30px;"+"-fx-max-height:30px;")
quitBtn.setLayoutX(_wndW - 50)
quitBtn.setLayoutY(20)
quitBtn.setOnAction(myBtnAction)
pane.getChildren().add(quitBtn)
Addendum: |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Can you try moving the exit_flag = False
def myBtnAction(event):
global exit_flag
exit_flag = True
...
def draw():
if exit_flag:
exit_sketch() MacOS is very particular about the main thread and will only allow that thread to perform certain actions. |
Beta Was this translation helpful? Give feedback.
-
The solution is to get the stage from the default window and then use stage.close(). # Uses Imported mode for py5
import javafx
_wndW = 400
_wndH = 200
def myBtnAction(event):
global stage
stage.close()
def setup():
global stage
size(_wndW,_wndH,FX2D)
get_surface().set_always_on_top(True)
window_title('JavaFX Round Button in Default Window')
canvas = get_surface().get_native()
root = canvas.getParent()
scene = root.getScene()
stage = scene.getWindow()
pane = javafx.scene.layout.Pane()
root.getChildren().add(pane)
quitBtn = javafx.scene.control.Button("Q")
quitBtn.setStyle("-fx-background-radius:5em;"+"-fx-min-width:30px;"+"-fx-min-height:30px;"+"-fx-max-width:30px;"+"-fx-max-height:30px;")
quitBtn.setLayoutX(_wndW - 50)
quitBtn.setLayoutY(20)
quitBtn.setOnAction(myBtnAction)
pane.getChildren().add(quitBtn) |
Beta Was this translation helpful? Give feedback.
-
I just tested the following code on Linux, MacOS, and Windows, and it worked on all of them. def setup():
py5.size(400, 400, py5.FX2D)
def draw():
py5.rect(py5.mouse_x, py5.mouse_y, 10, 10)
def key_pressed():
py5.exit_sketch() The Interesting that this matters. In any case, your solution is the right one for what you want to do. |
Beta Was this translation helpful? Give feedback.
The solution is to get the stage from the default window and then use stage.close().