官术网_书友最值得收藏!

Getting started – setting up the editor skeleton

Our first goal is to implement the broad visual elements of the text editor. As programmers, we have all used text editors to edit code. We are mostly aware of the common GUI elements of a text editor. So, without further ado, let's get started.

The first phase implements the Menu, Menubutton, Label, Button, Text, and Scrollbar widgets. Although we'll cover all of these in detail, you might find it helpful to look at the widget-specific options in the documentation of Tkinter maintained by its author, Frederick Lundh, at http://effbot.org/tkinterbook/. You can also use the interactive shell, as discussed in Chapter 1, Meet Tkinter.

You might also want to bookmark the official documentation page of Tcl/Tk at http://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm. This site includes the original Tcl/Tk reference. While it does not relate to Python, it provides a detailed overview of each widget and is a useful reference. Remember that Tkinter is just a wrapper around Tk.

In this iteration, we will complete the implementation of broader visual elements of the editor.

We will use the pack() geometry manager to place all the widgets. We have chosen the pack manager because it is ideally suited for the placing of widgets, either in a side-by-side or top-down position.

Fortunately, in a text editor, we have all the widgets placed either side by side or in top-down positions. Thus, it is beneficial to use the pack manager. We can do the same thing with the grid manager as well.

A note on code styling
One of the key insights of the Python community is that code is read much more often than it is written. Following good naming conventions and consistency in code styling are key to maintaining readable and scalable programs. We will try to stick to the official Python styling guide, which is specified in the PEP8 documentation at https://www.python.org/dev/peps/pep-0008.

Some important styling conventions that we will stick to include the following:

  • Use four spaces per indentation level
  • Variable and function names will be lowercase, with words separated by underscores
  • Class names will use the CapWords convention

Let's start by adding the Toplevel window using the following code:

from tkinter import Tk
root = Tk()
# all our code goes here
root.mainloop()

Note a slight difference in the way we import tkinter here. In the last chapter, we imported tkinter using this code:

import tkinter as tk

Since we used tk as an alias, we had to append the alias name to every call made to a class defined in Tkinter, as in tk.Tk(), tk.Frame, tk.Button, tk.END, and so on. 

From this chapter onward, we will directly import the individual class that we will need for a given program. So, now that we need the Tk() class from Tkinter, we directly import it into our namespace as:

from tkinter import Tk

This, in turn, means that we can now directly reference it as the Tk class in our program without needing to append any alias name to it as in root = Tk().

A third method is to import all (*) the classes from Tkinter into the namespace by using the following command:

from tkinter import *

The asterisk symbol means we want everything from tkinter to be imported into the namespace, regardless of whether we use it. This is, however, bad programming practice as it leads to namespace pollution. Furthermore, in larger programs, it can be hard to tell which module a particular class has been imported from, thus making debugging a difficult task.

主站蜘蛛池模板: 灯塔市| 叶城县| 柏乡县| 张家港市| 万盛区| 巴中市| 高安市| 庆阳市| 英山县| 邓州市| 大城县| 义马市| 澳门| 清原| 诸城市| 三原县| 扶风县| 静海县| 北票市| 富裕县| 新野县| 孟州市| 四平市| 巴东县| 洛南县| 延川县| 新竹市| 沽源县| 丰台区| 金平| 盖州市| 佛学| 嘉峪关市| 五家渠市| 临西县| 揭东县| 邳州市| 玛多县| 本溪| 黄冈市| 安达市|