Editors and IDEs
One of the first decisions you'll make when programming in any language is what tools you'll be using to edit and run your code. This is both an important decision and an area to invest time in learning how to use the tools.
You'll be able to find plenty of advice and opinions about Python editors and IDEs (Integrated Development Environments). While this is another opinion, it's one based on experience.... If you're going to be spending more than a week working on your PySimpleGUI project, it's worth time investing in learning to use an IDE.
An IDE like PyCharm can initially feel very overwhelming to approach, but it's not beyond the grasp of a complete beginner. If you can use Word or Excel, even a little, you will be able to use an IDE.
What to Look For in an Editor/IDE
For the best overall experience, look for these features when choosing an Editor/IDE:
- Syntax color highlighting
- Docstring integration
- Ease of navigating within your code
- Integration with Python interpreter
The big-picture point here is the efficient use of your time. If you have to save your file, exit your editor, run python, if there's an error then open the file with the error in your editor, etc, then it's going to not only take a very long time to write your program, you're not going to enjoy the experience. That "Fun Goal" quickly disappears.
PyCharm can feel very scary for a first-time programmer. TIP - think of these tools like spreadsheets. You don't need to know every feature in order to be very productive with them. If you use spreadsheets, you may not know the programs all that well. You may never look in the menus, but that doesn't stop you from finding them useful. It's the same way with these IDEs. Very rarely will you need to know more than a few capabilities such as how to run your program & how to perform code completion (Control+Space for more IDEs). You don't need to know the complex features.
PyCharm, Visual Studio, Wing
These 3 IDEs work well with PySimpleGUI. PyCharm and Wing in particular work with PySimpleGUI's docstrings. They provide you with documentation as you are writing your code, enabling you to stay focused, and using a single tool.
Opening a browser to look up documentation breaks your concentration. Having your editor perform code completion for you is a huge productivity boost.
Other Editors and IDEs
Here are a few others for you to consider.
Notepad++, Sublime, vim, Atom, Spyder, Thonny, PyDev, IDLE
Built into PySimpleGUI are docstrings. These are comments formatted in a particular way that enable editors to help you write your code. They are also used to generate the PySimpleGUI documentation.
Here is an example of PyCharm's docstring integration. You can see the documentation on the
Text element on the left side of the screen. In PyCharm, pressing Control+Q while on an object/function will show you this documentation.
And this is the same program shown in the Wing IDE. The same documentation on the
Text element is shown.
PyCharm Limitation with Aliases
PyCharm does not show the docstring if you are using an alias of an object (element). For example,
sg.Text has an alias of
sg.T. If you use
sg.T then PyCharm doesn't show you the docstring information.
Type Checking With Docstrings
PyCharm automatically does type-checking when using PySimpleGUI's docstrings. Here is an example of using a
Graph element. The first parameter should be a tuple with 2 ints:
This line of code is correct. The constant used in the first parameter is a tuple.
This line of code is attempting to use a string as the first parameter which is a mismatch. PyCharm highlights this problem before you have even attempted to run your program. This is a great example of the power an IDE has over a simple text editor. No special setup was required for this feature to work. The default settings provide this level of checking.
Again In PyCharm, if you type Control+P while entering parameters to a function/method, then you will see a pop-up window showing you all of the available parameters and what their default values are.
In most IDEs and editors, code completion is accomplished by pressing Control+Space. It may also happen automatically as you're typing. This feature is one of the most time saving features you'll encounter in programming. Not only does it enable you to type significantly less characters, it also shows you options available that you may not know about.
For Function Names
The Code Completion feature doesn't just apply to PySimpleGUI, it applies to every package/module you'll use, especially the built-in ones. Don't know all of the available methods for the
os.path. module? No problem, type in
os.path. and press Control+Space
This feature is particularly useful when using PySimpleGUI features that have a lot of functions that automatically set parameters for you to save you time. For example, if you want a
popup window without a titlebar, you can call
popup and set the
no_titlebar parameter to
True. Or, you can call
PySimpleGUI heavily uses named parameters and most calls in PySimpleGUI have a large number of parameters. Memorizing these parameters is out of the question.
Text element, for example, has 23 parameters. The
popup function also has 23 parameters. Because these are named parameters, you will need to specify the name when you call the function or create the object . You can either look up the names in the documentation, a very laborious task, or you can use an editor/IDE that will do it for you.
This example shows that the Text element has 2 parameters that are used to expand the element. As you are typing in the code, you'll be shown these 2 parameters. If the one you want is highlighted, you can press the Tab key and the editor will automatically insert the remainder of the text for you. In his example, 3 characters have been entered,
exp, if the Tab key was pressed then the text
expand_x= would be inserted into your code, thus saving you from having to type 3 characters instead of 9.
Jump To Definition
One final feature that you may find helpful if your editor does not support docstrings, or if you have a large project with many files. If you hold the control key and click on an object/function name, then your editor will open the file and jump to the location where that item is defined. This will allow you to view the definition of the call directly.
Caution Using IDLE (and maybe Spyder and Thonny)
IDLE, the editor/IDE that comes with Python, uses tkinter for its user interface. This generally isn't a problem, unless, you attempt to run your program more than once. If you run a PySimpleGUI program and exit it, and then try to run it again, then this can sometimes cause problems.
Spyder and Thonny have had some trouble as well. If the port of PySimpleGUI you are using matches the GUI used to create the editor, then you may run into problems when running your PySimpleGUI program from within the IDE.
Setup Your Editor in The PySimpleGUI Global Settings
Once you've setup your editor, be sure and tell PySimpleGUI about it. This will enable you to most effectively use the Demo Browser and integrate your editor with the PySimpleGUI error window's
"Take me to error" feature. This feature will open your editor to the line of code that has the error, even if you are not running your program using your editor/IDE.
For more info, check out: PySimpleGUI System Settings