Summary

In this chapter we have learned all about menus. There are many different ways in which the flexible Menu widget can be utilized, and we have practiced three.

We first created a main menu bar along the top of our application where we could provide access to functions of the editor. Afterwards we added menus into this bar by creating a cascade. This gave us File, Edit, Tools, and Help menus sitting at the top of our editor. We also looked at a solution that allows these items to auto-populate with functions and keyboard shortcuts providing we follow a naming convention for our editor's methods.

Finally, we created a separate right-click context menu. This allows the user to manipulate the text in our TextArea widget without having to know the keyboard shortcuts or move the mouse all the way to the top of the screen.

In order to create these Menus we learned about the two main ways of adding items—add_command and add_cascade. We saw that add_cascade allows us to create a submenu inside another menu by passing the label as the text to show and menu as the Menu widget to insert, and add_command allows us to call a function when the user clicks this entry, provided we pass that function to the command argument.

Creating the file menu allowed us to make use of the filedialog module. We made use of two functions from this module—askopenfilename and asksaveasfilename. We now know just how simple Tkinter makes it to get file path choices from a user who may not know how to build a full path to a file, since with this module they don't have to! We also got a glimpse of how to force a file type when asking the user to open a file, meaning they should not be able to pick an incorrect file and have the application behave in an unexpected way.

Additional Toplevel windows were added to our application and configured to share the color scheme of the TextEditor class itself. This also allowed us to see a practical example of how ttk's Style object allows for very easy application-wide styling of the widgets it includes, such as Label and Button widgets.

The colorchooser and font modules were briefly looked at, and we learned how to make use of one function in each—families and askcolor. We saw that the families function makes it super easy to get a tuple of all fonts available on the user's system, making font selection worry-free. Getting a color choice from a user was also made incredibly simple via the askcolor function—providing the user with sliders and a hex code entry so that they can see exactly what color they are selecting.

In the next chapter, we shall be moving on to an online chat application. Here we will be making use of the ever-powerful Canvas widget, as well as the Text widget we have learned a lot about from this project. We will eventually set up a chat server using the simple yet powerful flask module and maintain a list of friends using sqlite for permanent storage.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset