Large Text fields are harder to handle

So Text fields should work the same way, right? Well, actually, they don't. The designers of tkinter figured people might want to access particular lines and columns in Text fields. This means that while Entry fields use a single number to index the contents of the field, Text fields use a string, in the form row. column:

-first t^citr has index l.o"

9« the entire Contents.

my_large_field.get("1.0

This will return the entire Contents of the field-

This means from the and COLm^O, which .S the first character in the Ar.cld-<

END)

^This will return^ ALL of the text '» the Text field.

5tel.lor 1 rflsn the fellow's nair.ij. Abbott: Who.

L'stullc: Tha guy an tint. Abbott: Wha. Costal-.o: The first baseman. Abbott: who. Cos telle: The y plnying. . . Abbott: Who iji Dn first1 Costnllg: I'm asking YOU who's on first. Abbott: That's the n's name. Ccstello; That's vho's He™? Abbott! Yu=_ COxEollo: Wall gc aha id and tail si>. Abbott: at's it. Costello: That's who? Abbott: les.

Once you understand how Text indexes work, you'll see that you can insert and delete text from them in a very similar way to Entry fields:

my_large_field.delete("1.0", END) This will clear the field.

my_large_field.insert("1.0", "Some text")

Thiswilli^rtthe^tat the start of the f .eld-

Now that you know how to create text fields and control the text they contain, you're ready to build the Head-Ex application.

Be careful how you number rows and columns in Text () fields.

Rows begin at 1, but columns begin at 0.

create the interface

Code Magnets

Complete the code to create the interface. Think carefully about the widgets you will need for each of the fields.

from tkinter import *

You don't „eed f° k«P brat k of the labels, ieed to th

Create the GUI.

app = Tk() ÉT" app.title('Head-Ex Deliveries')

Recall that >ackO" adds

assign to variables.

depot = Entry (app^^uH want to keep track Calling "packO" without options depot. pack () of the data entry fields, SO means you leave it to tkinter assign them to variables.

to decide how best to lay things out on the GUI.

^This means the button

Button(app, text = "Save", command = save_data).pack() will ¿all the save data''

function when clicked-

n your pencil_

In addition to the GUI code, you need to write the function that will save the data from the GUI to the deliveries . txt file.

The Save button on the interface is going to call a function called save_data (). The function will append the data from the GUI to the end of the deliveries . txt file formatted like this:

Depot : Seattle, WA Description: Books Address: 1 Main Street Anytown WA

Then it will need to clear the fields on the form to make them ready for the next record to be entered. The function will have to appear in the program before the GUI code. Write the code for the function here:

Pon'tW^^f needs to save the data-

the fields-

interface created

Code Magnets Solution

You were to complete the code to create the interface. You were to think carefully about the widgets you will need for each of the fields.

from tkinter import app = Tk( )

app.title('Head-Ex Deliveries') Label(app, text = "Depot:").pack() depot = Entry(app) depot.pack()

from tkinter import app = Tk( )

app.title('Head-Ex Deliveries') Label(app, text = "Depot:").pack() depot = Entry(app) depot.pack()

I Label(app, text = "Address:") | Lpack()

Create two fields: one is an Entry f ield | description^||. pack () and the other is a Text field.

I Label(app, text = "Address:") | Lpack()

Don't forget to pack those widget^

is a lavgev Text f ield-

Button(app, text = "Save", command = save_data).pack()

The Save button on the interface is going to call a function called save_data (). The function will append the data from the GUI to the end of the deliveries . txt file formatted like this:

Then it will need to clear the fields on the form ready for the next record to be entered. The function will have to appear in the program before the GUI code. You were to write the code for the function here:

YS c°dfray4lo°k a Append the text to the end of defile, lttle different- ^ jT as you did when we wrote the PCS

fileD = open("deliveries.txt", "a")

get() returns the . fileD:write("Depot:\n")

of an Entry . fileD.write("%s\n" % depot.get()) fileD.write("Description:\n") fileD.write("%s\n" % descripWgetO) fileD.write("Address:\n")

fileD.write("%s\n" % address.getH.0", END))

idepotdelete(0, END) descriptiondelete(0, END)

address.delete("l.0", END)

This means "1st row, 0th column." Remember that rows start at 1 and columns from 0.

Was this article helpful?

0 0

Post a comment