Fun with Application Menus

Image by ZennDogg

My use of Python entails mostly modifying my PC to my liking. Above is a screenshot of the home screen on my computer. Across the top are two menu bars and a clock/weather display.

The menu items are general categories with drop-down menus to choose a specific app. For instance, when clicking on EDITORS, the following drop-down is displayed.

This article concentrates on the code for the drop-down menu applications. The full code can be found at the end. As usual, I will be using PyQt5 as my GUI package of choice. The Python version is 3.8. We’ll start with the import statements.

qtpy_cfg is a mixin file of PyQt5 functions I wrote for procedures that I use often. The three functions involved will be explained later.

Next, we write what is probably the shortest python class ever written.

As you can see, this class sends a signal to PyQt5 to close an app using pyqtSignal(). We call this to close the drop-down menu once a selection has been made.

The next class is a threading class. Each menu selection is run in its own thread.

I use this generic class in all my GUI apps. Every menu item is either a web application or stand-alone software program, so this gets used…a lot.

The last class is the GUI class. Every drop-down menu has different custom sizes (note: even though QtDesigner is easier, it’s also more generic. I hard-code every aspect of my projects. Gotta be me).

After determining the size and position of the app, self.comm initializes the Communicate class and the following line of code assigns the signal to close(). As we go through this project, you may notice we won’t have a function named close(). That’s because close() is an internal function of PyQt5.

Next, CloneThread() is initialized.

The following two images explain the files imported from my helper file. qblack() is a function that formats the GUI window, which is all black with no title bar or any other parts of a normal display window. Its code is below.

The next line of our code initializes qbutton_dim(). The function forms the background and foreground colors for the menu items display (buttons).

Getting back to the main code, we next assign the font we want to use, Candalara with a font size of 9.

This next section of the code defines the four menu items in the drop-down menu.

qtbutton() defines each menu item’s name, size, style, font, display name, and connection to a function that performs the required action, all in one line of code.

The following image is the code for a button in PyQt5 (minus the first line). It’s six lines long! I make a lot of buttons which led me to codify the process.

Next, we need a timer to automatically close the menu app if no selection is made.

Now it’s time to define some functions. These four functions can be found in every one of my menu apps and are mostly for clean-up and consistent operation of all drop-down menus.

mousePressEvent() is an internal function to PyQt5. Click the left mouse button anywhere on the menu display and the closeApp() signal is emitted.

close_menu() starts the timer which runs for 6.5 seconds, after which mousePressEvent() is called and the menu closes.

wrap_up() wraps up the operation of the menu. The reason for the num variable is to give the program enough time to execute the menu item call before moving through the next two lines of code. I found I had to incorporate a time delay of 0.6 seconds for web calls to work. 0.1 or 0.2 seconds works for software calls. mousePressEvent() is called to close the menu if a selection was made. If no selection was made, close_menu() closes it after 6.5 seconds. Lastly, we define the threads() function.

Next are the functions for the four menu items in our drop-down menu.

Two lines of code are all it takes. We call the selected program, then wrap it up.

Now for the last part of our code.

That’s it. Every drop-down menu uses this same basic program.

I hope you enjoyed reading. Please leave a comment. I am open to compliments, criticism, and chocolate chip cookies.

Full code follows:

Here are the helper functions:

Retired, Self taught in python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store