-
The following demo creates a source code launcher using subprocess. When 'python3' is used with subprocess the app runs as expected. When 'py5-run-sketch' is used with subprocess the app hangs and must be Force Quit on a Mac. To aid in testing, sample files for both cases follow the demo. You should be able to run the python3 example files in succession without error. The py5-run-sketch example code allows only one run; the app hangs and must be Force Quit in order to continue. py5-run-sketch is an executable file and I am unable to find the source code in an attempt to find an explanation for this behavior. import javafx
import subprocess
import os
_wndW = 500
_wndH = 280
def cBox1Action(event):
global cBox1,cBox2,cmdFld,fileFld
if(cBox1.isSelected):
cmdFld.setText("python3 ")
cBox2.setSelected(False)
def cBox2Action(event):
global cBox1,cBox2,cmdFld,fileFld
if(cBox2.isSelected):
cmdFld.setText("py5-run-sketch ")
cBox1.setSelected(False)
def quitBtnAction(event):
global stage
stage.close()
def selectFileAction(event):
global fileFld, cmdFld, runStr, runFld
fileChooser = javafx.stage.FileChooser()
fileChooser.getExtensionFilters().add(fileChooser.ExtensionFilter("Python Files","*.py"))
selectedFile = fileChooser.showOpenDialog(stage)
fileStr = selectedFile.getAbsolutePath()
fileFld.setText(fileStr)
cmdStr = cmdFld.getText()
runStr = ('%s %s') % (cmdStr,fileStr)
runFld.setText(runStr)
def runAction(event):
global runStr
try:
s = subprocess.check_output(runStr, shell=True)
except subprocess.CalledProcessError as e:
print(f"Command failed with error: {e}")
def setup():
global stage, cBox1, cBox2, cmdFld, fileFld, runFld
size(_wndW,_wndH,FX2D)
window_title('py5-run-sketch Tester')
canvas = get_surface().get_native()
root = canvas.getParent()
scene = root.getScene()
stage = scene.getWindow()
stage.setAlwaysOnTop(True)
pane = javafx.scene.layout.Pane()
font = javafx.scene.text.Font.font("Menlo", javafx.scene.text.FontWeight.BOLD, javafx.scene.text.FontPosture.REGULAR, 14)
# **** Run Btn ****
arrw = javafx.scene.shape.Polygon()
arrw.getPoints().addAll(
90.0, 52.0,
90.0, 68.0,
100.0, 60.0
)
arrw.setStroke(javafx.scene.paint.Color.WHITE)
arrw.setFill(javafx.scene.paint.Color.BLUE)
runBtn = javafx.scene.control.Button()
runBtn.setStyle("-fx-background-radius:5em;"+
"-fx-min-width:24px;"+
"-fx-min-height:24px;"+
"-fx-max-width:30px;"+
"-fx-max-height:30px;"
)
runBtn.setGraphic(arrw)
runBtn.setLayoutX(_wndW - 60)
runBtn.setLayoutY(180)
runBtn.setOnAction(runAction)
# python3 CheckBox
cBox1 = javafx.scene.control.CheckBox("python3")
cBox1.setFont(font)
cBox1.setIndeterminate(False)
cBox1.setLayoutX(30)
cBox1.setLayoutY(10)
cBox1.setOnAction(cBox1Action)
# py5-run-sketch CheckBox
cBox2 = javafx.scene.control.CheckBox("py5-run-sketch")
cBox2.setFont(font)
cBox2.setIndeterminate(False)
cBox2.setLayoutX(180)
cBox2.setLayoutY(10)
cBox2.setOnAction(cBox2Action)
# Quit Btn
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.setOnAction(quitBtnAction)
quitBtn.setLayoutX(_wndW - 50)
quitBtn.setLayoutY(10)
# CmdStr Label
cmdLabel = javafx.scene.control.Label("cmdStr")
cmdLabel.setFont(font)
cmdLabel.setLayoutX(30)
cmdLabel.setLayoutY(45)
# Cmd Field
cmdFld = javafx.scene.control.TextField()
cmdFld.setLayoutX(30)
cmdFld.setLayoutY(70)
cmdFld.setPrefWidth(150)
# SelectFile Button
selectFileBtn = javafx.scene.control.Button("SelectFile...")
selectFileBtn.setFont(font)
selectFileBtn.setLayoutX(230)
selectFileBtn.setLayoutY(70)
selectFileBtn.setOnAction(selectFileAction)
# FileStr Label
fileLabel = javafx.scene.control.Label("fileStr")
fileLabel.setFont(font)
fileLabel.setLayoutX(30)
fileLabel.setLayoutY(115)
# File Field
fileFld = javafx.scene.control.TextField()
fileFld.setLayoutX(30)
fileFld.setLayoutY(140)
fileFld.setPrefWidth(_wndW - 60)
# RunStr Label
runLabel = javafx.scene.control.Label("runStr = cmdStr + fileStr")
runLabel.setFont(font)
runLabel.setLayoutX(30)
runLabel.setLayoutY(195)
# Run Field
runFld = javafx.scene.control.TextField()
runFld.setLayoutX(30)
runFld.setLayoutY(215)
runFld.setPrefWidth(_wndW - 60)
pane.getChildren().addAll(runBtn,cBox1,cBox2,quitBtn,cmdLabel,cmdFld,selectFileBtn,fileLabel,fileFld,runLabel,runFld)
root.getChildren().add(pane) python3 examples:
import py5
def setup():
py5.size(300, 200)
py5.rect_mode(py5.CENTER)
def draw():
py5.rect(py5.mouse_x, py5.mouse_y, 10, 10)
py5.run_sketch()
from tkinter import *
wnd = Tk()
C = Canvas(wnd, bg = "yellow", height = 250, width = 300)
line = C.create_line(108, 120, 320, 40, fill = "green")
arc = C.create_arc(180, 150, 80, 210, start=0, extent=220, fill = "red")
oval = C.create_oval(80, 30, 140, 150, fill = "blue")
C.pack()
mainloop()
import math
import matplotlib.pyplot as plt
golden=1.61803398875
# golden=1.325
radind=golden**(1/180)
totalDegrees=360
degree=math.pi/180
radius=radind
x=[]
y=[]
for angle in range(0,totalDegrees*4):
radians=degree*angle
x.append(
math.cos(radians)*
radius)
y.append(
math.sin(radians)*
radius)
radius*=radind
# plt.figure(figsize=(5,5))
plt.plot(x,y)
plt.show()
py5-run-sketch example:
# Uses Imported mode for py5
import javafx
_wndW = 600
_wndH = 600
def setup():
global slider,colorPicker
size(_wndW, _wndH, FX2D)
window_title("JavaFX DotView Default Window")
canvas = get_surface().get_native()
root = canvas.getParent()
pane = javafx.scene.layout.Pane()
slider = javafx.scene.control.Slider(20, 500, 10)
slider.setLayoutX(190)
slider.setLayoutY(20)
colorPicker = javafx.scene.control.ColorPicker(javafx.scene.paint.Color.GREEN)
colorPicker.setLayoutX(400)
colorPicker.setLayoutY(15)
pane.getChildren().addAll(slider,colorPicker)
root.getChildren().add(pane)
def draw():
global slider, colorPicker
background(209)
Color = colorPicker.getValue()
fill(int(Color.getRed()*255),int(Color.getGreen()*255),int(Color.getBlue()*255))
circle(_wndW/2,_wndH/2, float(slider.getValue())) Just now saw this error message in Thonny:
AI Overview says: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Popen is non-blocking. Changing subprocess to Popen stops the beach balling and allows multiple files to be opened. def runAction(event):
global runStr
try:
proc = subprocess.Popen(runStr, shell=True)
except Exception as e:
print(f"Error: {e}") |
Beta Was this translation helpful? Give feedback.
Popen is non-blocking. Changing subprocess to Popen stops the beach balling and allows multiple files to be opened.