The model stays the same

Think for a moment about the model you are using with the radio buttons. It represents the name of the chosen depot, which you want to keep for the option menu. If the model stays the same, then your code, which currently looks like this:

depot = StringVar() depot.set(None)

Radiobutton(app, variable = depot, text = "Cambridge, MA", value = "Cambridge, MA").pack() Radiobutton(app, variable = depot, text = "Cambridge, UK", value = "Cambridge, UK").pack() Radiobutton(app, variable = depot, text = "Seattle, WA", value = "Seattle, WA").pack()

can be replaced with code like this:

depot = StringVar() depot.set(None) OptionMenu(app, depot, t

The setond parameter must be -the model and it do^t need the "variable - bit

'Cambridge, MA", "Cambridge, UK", "Seattle, WA").pack()

A hst of all the options that appear in the widget

■This is the same model as before.

A hst of all the options that appear in the widget

■This is the same model as before.

Put wait... you don't have to list aH the values like that

It looks like a lot of work to put all of the options in the actual function call to OptionMenu (), doesn't it? After all, there's a large list of depots.

Thankfully, Python comes to the rescue. If you have the options already stored in a list:

depots = ["Cambridge, MA", "Cambridge, UK", "Seattle, WA"]

C_arn bridge, MA Cambridge, UK Seattle, Y/A New York, NY Dallas, TX Boston, MA ¡Rome, Italy Male, Maldives Luxor, igypt .Rhodes, Greece Edinburgh, Scotland

you can pass the entire list instead of separate values like this:

OptionMenu(app, depot, *depots).pack()

Now let's put the pieces together.

TV« * -ans "Take the rest of • the parameters from th.s l.st and insert them here.

adding options

long ExidâSe

This is a version of the program that uses radio buttons.

You are going to update this program so that it uses an option menu. But the options need to be read from a text file.

from tkinter import *

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

fileD.write( fileD.write( fileD.write( fileD.write( fileD.write( fileD.write(

%s\n" % depot.get()) Description:\n" ) %s\n" % description.get() ) Address:\n")

depot.set(None)

description.delete(0, END)

description.delete(0, END)

address.delete("1.0", END)

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

depot.

Radiobutton(app, variable pack ( )

Radiobutton(app, variable pack()

Radiobutton(app, variable ()

depot, text = "Cambridge, MA", value depot, text = "Cambridge, UK", value depot, text = "Seattle, WA",

Label(app, text = "Description:").pack() description = Entry(app) description.pack()

Label(app, text = "Address:").pack() address = Text(app) address.pack()

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

value depot, text = "Cambridge, MA", value depot, text = "Cambridge, UK", value depot, text = "Seattle, WA",

value

First, you need to create a function called read_depots () that will read the lines in a text file and return them to your code as a list.

Hint: When you read a line from the file, it might have a newline character at the end. The rstrip () string method will remove it for you.

Then, you need to replace this section of the code with code that generates an option menu using the data from the Q read_depots ( ) function you just created. It should use a file called depots . txt. Write the code here:

options added to h

ExcRciSe Solution

This is a version of the program that uses radio buttons.

You needed to update this program so that it used an option menu. But the options were to be read from a text file.

from tkinter import *

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

fileD.write( fileD.write( fileD.write( fileD.write( fileD.write( fileD.write(

%s\n" % depot.get()) Description:\n") %s\n" % description.get()) Address :\n")

depot.set(None)

description.delete(0, END)

description.delete(0, END)

address.delete("1.0", END)

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

Radiobutton(app, variable = depot, text = "Cambridge, MA", value pack()

Radiobutton(app, variable = depot, text = "Cambridge, UK", value pack()

Radiobutton(app, variable = depot, text = "Seattle, WA"

'Cambridge, value

Label(app, text = "Description:").pack () description = Entry(app) description.pack()

Label(app, text = "Address:").pack() address = Text(app) address.pack()

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

^ First, you needed to create a function called read_depots () to read the lines in a text file and return them as a list.

Open -the file"

Read from the file one__

line at a time. . for . line, .in . de.po.ts_f.:

Append a stripped copy of- depots.append(line.rstrip())

the line to the array.

' return depots

Return the list to the talli^ Code

Start with an empty array-

Then, you needed to replace this section of the code with code that generated an option menu using the data returned ^ by the read_depots () function. It needed to use a file called depots . txt. You were to write the code here:

foil the function, passing in the name of the file to read the dat»

from. options = read_depots("depots.txt")

Use the data to Wild OpW^nu^pp, depot, *°ptions).packO

option menu-

Was this article helpful?

0 0

Post a comment