PySimpleGUI Tutorial 5: Building an Executable Text Editor Application

Posted by


In this PySimpleGUI tutorial, we will be creating a simple text editor executable app using PySimpleGUI library. Text editors are commonly used applications for reading, writing, and editing text files.

Step 1: Install PySimpleGUI
First, you need to install PySimpleGUI library if you haven’t done so already. You can install PySimpleGUI using pip by running the following command in your terminal or command prompt:

pip install PySimpleGUI

Step 2: Import necessary libraries
Next, you need to import the PySimpleGUI library and any other libraries that you will need for your text editor application. In this tutorial, we will be using the sg alias for PySimpleGUI library to make the code less verbose.

import PySimpleGUI as sg

Step 3: Create a layout for the text editor
Now, let’s create a layout for our text editor window. We will use a Text element to display the text and a Multiline input element for the user to type in. We will also add a Save button to save the text to a file.

layout = [
    [sg.Text('Enter text below:')],
    [sg.Multiline(size=(70, 20), key='-TEXT-')],
    [sg.Button('Save')]
]

Step 4: Create a PySimpleGUI window
Next, we need to create a PySimpleGUI window using the layout we created in the previous step. We will also set the title of the window to ‘Text Editor’.

window = sg.Window('Text Editor', layout)

Step 5: Main event loop
Now, we will create a main event loop to handle user input in the text editor window. The loop will run until the user closes the window by clicking the ‘X’ button or by clicking the ‘Exit’ button.

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED or event == 'Exit':
        break

window.close()

Step 6: Save button functionality
In the main event loop, we will check if the user clicks the ‘Save’ button and if they do, we will save the text in the Multiline input element to a file named ‘text.txt’.

if event == 'Save':
    text = values['-TEXT-']

    with open('text.txt', 'w') as f:
        f.write(text)
        sg.popup('Text saved to file.')

Step 7: Creating an executable app
To create an executable app from your text editor script, you can use PyInstaller. First, install PyInstaller using pip:

pip install pyinstaller

Then, run the following command in your terminal or command prompt to create an executable app:

pyinstaller --onefile your_script.py

Replace your_script.py with the name of your text editor script.

And that’s it! You have created a simple text editor executable app using PySimpleGUI library. You can now run the executable app on any machine without needing to install Python or any additional libraries. Feel free to customize the text editor further by adding more functionality like open, save as, or edit features.

0 0 votes
Article Rating

Leave a Reply

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@cnlion3360
7 hours ago

source:
# Updating windows after event

import os

import PySimpleGUI as pg

# Step 1: Set theme

pg.theme("default1")

# Step 2: Create layout

file_list_column = [

[

pg.Text("Folder"),

pg.In(size=(30, 1), enable_events=True, key="-FOLDER-"),

pg.FolderBrowse(),

],

[

pg.Listbox(

values=[],

enable_events=True,

size=(50, 20),

key="-FILE_LIST-"

)

]

]

file_viewer_column = [

[pg.Text("Choose a file from the list", size=(50, 1))],

[pg.Text("File name: ", size=(70, 3), key="-TOUT-")],

[pg.Multiline(size=(70, 30), key="-TEXT-")]

[pg.Button("Save")]]

layout = [

[

pg.Column(file_list_column),

pg.VSeperator(),

pg.Column(file_viewer_column)

]

]

# Step 3: Create Window

window = pg.Window("File Viewer", layout)

# Step 4: Event loop

folder_location = ""

while True:

event, values = window.read()

if event == pg.WIN_CLOSED or event == "Exit":

break

elif event == "-FOLDER-":

folder_location = values["-FOLDER-"]

try:

files = os.listdir(folder_location)

except:

files = []

file_names = [

file for file in files

if os.path.isfile(os.path.join(folder_location, file))

and file.lower().endswith((".txt", ".csv", ".json", ".py"))

]

window["-FILE_LIST-"].update(file_names)

elif event == "-FILE_LIST-" and len(values["-FILE_LIST-"]) > 0:

file_selection = values["-FILE_LIST-"][0]

with open(os.path.join(folder_location, file_selection)) as file:

contents = file.read()

window["-TOUT-"].update(os.path.join(folder_location, file_selection))

window["-TEXT-"].update(contents)

elif event == "Save" and len(values["-FILE_LIST-"]) > 0:

with open(os.path.join(folder_location, file_selection), "w" ) as file:

file.write(values["-TEXT-"])

# Step 5: Close window

window.close()

exit()

@ashG1234
7 hours ago

Hi Can you demonstrate how to create a table which could be used to enter data – parent/child relation ship. For example I want to create a new class and add 2 or more students to the class along with other informations, say assigned group name etc. I should be able to select student name from a drop down in the table.

@suhaibhassan9607
7 hours ago

Great work. Superb. You are great.

3
0
Would love your thoughts, please comment.x
()
x