DEV Community

Cover image for How to launch a GUI application using PySimpleGUI with a double-click on a mac
Yuki Shindo
Yuki Shindo

Posted on

How to launch a GUI application using PySimpleGUI with a double-click on a mac

On mac, by creating a .sh file in the form of .command, you can execute that shell script file by double-clicking it.

Using this mechanism, I tried to launch a GUI application using PySimpleGUI by double-clicking the file.

About the PySimple GUI application to run

Create a simple PySimpleGUI application that displays a list of theme colors provided by PySimpleGUI.

Save this code as display-theme.py.

import PySimpleGUI as sg
sg.theme_previewer()
Enter fullscreen mode Exit fullscreen mode

When you run this code, you will see a screen like this.

Displays a list of theme colors provided by PySimpleGUI

Create a .command file

Let's actually create a .command file.

Create the .command file directly like this.

vim display-theme.command
Enter fullscreen mode Exit fullscreen mode

Or, you can create a .sh file, write your code in it, and then change it to .command.

mv display-theme.sh display-theme.command
Enter fullscreen mode Exit fullscreen mode

Contents of the shell script

# Chnage current dir
cd `dirname $0`

# Run python script
python display-theme.py
echo $?

# Close the terminal by pressing any key
read a
Enter fullscreen mode Exit fullscreen mode

When the .command file is executed, the current directory is set to root, so this command will take you to the location where the file exists.

cd `dirname $0`
Enter fullscreen mode Exit fullscreen mode

This code is written as a confirmation. It just prints out an echo to see if the execution is completed successfully.

echo $?
Enter fullscreen mode Exit fullscreen mode

By including this description, the terminal will be closed by pressing any of the keys.
If this is not included, the terminal will close instantly, so it is also add to make sure that it was executed.

read a
Enter fullscreen mode Exit fullscreen mode

File permission

Once you have created the shell script, give it execution permissions like this.

chmod u+x display-theme.command
Enter fullscreen mode Exit fullscreen mode

Double-click

At this point, your file directory should look like this.

.
├── display-theme.command
└── display-theme.py
Enter fullscreen mode Exit fullscreen mode

You can then double-click this .command file directly from the Finder to launch the GUI application with the theme color list.

This sample code is also available on GitHub.

shinshin86/pysimplegui-mac-app-sample(GitHub)

Top comments (0)