Windows management system for python GUI explained


This explanation is about previously published tutorial Build simple Books Manager application with Python and Tkinter from scratch.

Application has one main window and two context windows. Those context windows are opening whenever user clicks on 'New book' and 'Edit book' button on the left side of the main app window. 

The goal here is to prevent user to click those buttons again or to select another item from book list when any of the context windows are open.

This is done using two things. First is context_open property from main Library class. At the end of the __init__ function we declare this property to be false with:


self.context_open = False


We do this because when main window is created for the first time no context window is open. 

Second thing is self.config() function.



Function config is called when any of the context window opens. So what happens inside config function?

If context_open is false, we first set:


root.protocol('WM_DELETE_WINDOW', lambda:0)


to disable root windows close button. If we don't do this, when user closes root window, context window will stay open hanging from closed application, which is a major bug. You can comment out this line to see this bug in action.

We also set buttons state to disabled and with 


tree.config(selectmode="none") 


we don't allow user to change list selection.

If, on the other hand, context_open is true, that means we closed context window and we need to restore state parameters for buttons, than restore option for user to be able to select item from the list and with:


root.protocol('WM_DELETE_WINDOW', root.destroy)


we restore root window close button functionality.

Last line of config function acts like a switch.


self.context_open = not self.context_open


It changes value of context_open variable from False to True and vice versa.

I hope this makes clear how windows management system works.


Thanks for reading.


Post a Comment

0 Comments