Web Analytics Made Easy - Statcounter
Skip to content

Coding Conventions

The primary suggested conventions are:

  • import PySimpleGUI as sg
  • Name your Window window
  • Name the return values from reading your window event and values
  • Name your layout layout
  • Always check for Window Closed event immediately following window.read() calls
  • Use window[key] to lookup elements
  • For keys that are strings, follow this pattern '-KEY-'

Why Have Conventions

Throughout the PySimpleGUI documentation, the Demo Programs, etc, you will find that all programs follow the same naming conventions.

By following some simple coding conventions you'll be able to copy / paste demo program code into your code with minimal or no modifications. Your code will be understandable by other PySimpleGUI programmers.

Of course, these are suggestions and you've got a choice. They've been used almost universally over the past few years, probably because:

  • They're intuitive and logical
  • You benefit from following them
    • You'll become accustomed to seeing the same names and conventions found in these documents as in your code.
    • When asking for help, others will understand your code much more quickly.
  • You will help the PySimpleGUI project
    • If a feature you're using is going to be changed, your code will be visible if that feature is searched for and you followed the naming convnentions
    • Your code can more easily be used by other programmers

You've not been introduced to some of these terms. It's not important that you understand what they mean.

Examples

Knowing these conventions, you'll be able to read portions of programs and understand what they mean. If you see this single line of code:

sg.popup('This is a popup')

then you know already what the sg. means at the start of the statement. You've learned that the import statement for PySimpleGUI programs is suggested to be:

import PySimpleGUI as sg

You'll instantly understand not only what these 2 statements mean, but you'll also know where in a PySimpleGUI you'll find them.

if event == 'Ok':
    name = values['-NAME-']

Coding and Naming Conventions are not Uncommon

If you have a software development job then your software team has established coding conventions to some degree. Standardization is a good practice in a development team.

Like anything, it's possible to over-do it with coding conventions. Killing your creativity is not the goal of these simple guidelines. There are SO many other ways you can be creative and expressive using PySimpleGUI than these few conventions.

PEP8

The Python language also has naming conventions that you'll find published as PEP8. The first release of PySimpleGUI did not follow PEP8 (due to lack of awareness), but a week after learning about PEP8, release 2.0 was hastily put together that corrected the naming errors