- Python 2.6 Graphics Cookbook
- Mike Ohlson de Fine
- 734字
- 2021-04-09 22:39:39
In a very similar manner to the way attributes are specified for lines and shapes, font type, size, and color are governed by the attributes of the create_text()
method.
The instructions used in recipe 1 should be used.
Just use the name 4color_text.py
when you write, save, and execute this program.
# 4color_text.py #>>>>>>>>>>>>>> from Tkinter import * root = Tk() root.title('Four color text') cw = 500 # canvas width ch = 140 # canvas height canvas_1 = Canvas(root, width=cw, height=ch, background="white") canvas_1.grid(row=0, column=1) xy = 200, 20 canvas_1.create_text(200, 20, text=" text normal SansSerif 20", \ fill='red',\ width=500, font='SansSerif 20 ') canvas_1.create_text(200, 50, text=" text normal Arial 20", \ fill='blue',\ width=500, font='Arial 20 ') canvas_1.create_text(200, 80, text=" text bold Courier 20", \ fill='green',\ width=500, font='Courier 20 bold') canvas_1.create_text(200, 110, text=" bold italic BookAntiqua 20",\ fill='violet', width=500, font='Bookantiqua 20 bold') root.mainloop() #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
The results are given in the following screenshot:
A difficulty in specifying fonts is deciding which fonts are best for your needs. Once you have selected a font, you may discover that your particular operating system does not support that font. Fortunately, the designers of Tkinter made it somewhat bulletproof by causing it to select a suitable default font if the one you specified was not available.
Placement of text – north, south, east, west.
We place text on a canvas using the position specifiers that Tkinter has available. Anchor positions, text x, y location, font size, column width, and text justification all interact to control the appearance of text on the page. The following screenshot shows the compass nomenclature used in positioning the text:
Placing text onto a canvas is tricky until we understand the navigation system that Tkinter uses. Here is how it works. All text goes into an invisible box. The box is like an empty picture frame placed over a nail on a board. The Tkinter canvas is the board and the empty frame is the box that the text we type is going to fit inside. The nail is the x and y location. The empty frame can be moved so that the nail is in the top left-corner (North-West) or the bottom right (South-East) or in the center or the other corners or sides. The following screenshot shows the imaginary frame on the canvas that contains the text:
Execute the code and observe how the various text position specifiers
control the appearance of the text.
# anchor_align_text_1.py #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> from Tkinter import * root = Tk() root.title('Anchoring Text North, South, East, West') cw = 650 # canvas width ch = 180 # canvas height canvas_1 = Canvas(root, width=cw, height=ch, background="white") canvas_1.grid(row=0, column=1) orig_x = 220 orig_y = 20 offset_y = 30 # 1. DEFAULT CENTER JUSTIFICATION # width is maximum line length. canvas_1.create_text(orig_x, orig_y , \ text="1===|===10", fill='red', width=700, font='SansSerif 20 ') canvas_1.create_text(orig_x, orig_y + offset_y, \ text="1. CENTER anchor", fill='red', width=700, font='SansSerif 20 \ ') canvas_1.create_text(orig_x, orig_y + 2 *offset_y, \ text="text Arial 20", fill='blue', width=700, font='SansSerif 20 ') #=================================================================== orig_x = 380 # 2. LEFT JUSTIFICATION canvas_1.create_text(orig_x, orig_y, text="1===|===10",\ fill='black', anchor = NW, width=700, font='SansSerif 16 ') canvas_1.create_text(orig_x, orig_y + offset_y, text="2. NORTH-WEST \ anchor",\ fill='black', anchor = NW, width=700, font='SansSerif 16 ') canvas_1.create_text(orig_x, orig_y + 2 *offset_y, fill='black',\ text="text SansSerif 16", anchor = NW, width=700, font='SansSerif \ 16 ') #================================================================== # 3. DEFAULT TOP-LEFT (NW) JUSTIFICATION orig_x = 170 orig_y = 102 offset_y = 20 canvas_1.create_text(orig_x, orig_y , anchor = NW ,text="1===|===10",\ fill='green', width=500, font='SansSerif 10 ') canvas_1.create_text(orig_x, orig_y + 1 * offset_y, anchor = NW ,\ text="3. NORTH-WEST anchor", fill='green', width=500, \ font='SansSerif 10 ') #canvas_1.create_text(orig_x, orig_y + 2 * offset_y, anchor = NW,\ #text="abc", fill='green', width=700, font='SansSerif 10 ') canvas_1.create_text(orig_x, orig_y + 2 * offset_y, anchor = NW, \ text="abcde", fill='green', width=500, font='Bookantiqua 10 bold') #=================================================================== # 4. DEFAULT Top-right (SE) JUSTIFICATION canvas_1.create_text(orig_x, orig_y , anchor = NE ,\ text="1===|===10", fill='violet', width=500, font='SansSerif 10 ') canvas_1.create_text(orig_x, orig_y + 1 * offset_y, anchor = NE ,\ text="4. NORTH-EAST anchor", fill='violet', width=500, \ font='SansSerif 10 ') #canvas_1.create_text(orig_x, orig_y + 2 * offset_y, anchor = NE, \ #text="abc",fill='violet', width=700, font='SansSerif 10 ') canvas_1.create_text(orig_x, orig_y + 2 * offset_y, anchor = NE,\ text="abcdefghijklmno", fill='violet', width=500, font='Bookantiqua 10 bold') root.mainloop() #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>