JCA Python-Middle Lesson 04 1555673909
JCA Python-Middle Lesson 04 1555673909
Creating Windows
and Menus,
Options for Saving.
To-Do App Development
CONTENTS
Menus in Window Apps. Creating a Notepad App. . . . . . 3
To-Do App Development . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2
Creating
Creating Windows
Windows andand Menus,
Menus, Options
Options forfor Saving.
Saving. To-DoApp
To-Do AppDevelopment
Development
3
Lesson # 4
Lesson # 4
Figure 1
Figure 2
4
Creating
Creating Windows
Windows andand Menus,
Menus, Options
Options forfor Saving.
Saving. To-DoApp
To-Do AppDevelopment
Development
Figure 3
5
Lesson # 4
Lesson # 4
Figure 4
6
Creating
Creating Windows
Windows andand Menus,
Menus, Options
Options forfor Saving.
Saving. To-DoApp
To-Do AppDevelopment
Development
def open_file():
7
Lesson # 4
Lesson # 4
Add the menu to the panel under the name of the app
using the add_cascade() method:
menu.add_cascade(label='File', menu=file_menu)
menu.add_cascade(label='Help', menu=help_menu)
Figure 5
Figure 6
9
Lesson # 4
Lesson # 4
10
Creating
Creating Windows
Windows andand Menus,
Menus, Options
Options forfor Saving.
Saving. To-DoApp
To-Do AppDevelopment
Development
Figure 7
Write the function that will open files. This time we will
use the askopenfilename() method that opens the dialog
box. The content of the .txt file will be displayed in the text
area thus making our app a real notepad.
def open_file():
file_name = filedialog.
askopenfilename(initialdir='/', title='Open file',
filetypes=(('Text Documents',
'*.txt'), ('all files', '*.*')))
if file_name:
f = open(file_name, 'r')
text_open = f.read()
11
Lesson # 4
Lesson # 4
if text_open != NONE:
text.delete(1.0, END)
text.insert(END, text_open)
You can expand the app menu as you like and add a
certain command to each of the items.
Figure 8
12
Creating
Creating Windows
Windows andand Menus,
Menus, Options
Options forfor Saving.
Saving. To-DoApp
To-Do AppDevelopment
Development
Figure 9
13
Lesson # 4
Lesson # 4
Figure 10
14
Creating
Creating Windows
Windows andand Menus,
Menus, Options
Options forfor Saving.
Saving. To-DoApp
To-Do AppDevelopment
Development
def add_task():
def del_one():
def del_all():
def sort_asc():
def sort_desc():
def choose_random():
def show_number_of_tasks():
15
Lesson # 4
Lesson # 4
Set a style, font size, and background color for all ele-
ments:
root.option_add('*Font', '{Comic Sans MS} 10')
root.option_add('*Background', 'white')
16
Creating
Creating Windows
Windows andand Menus,
Menus, Options
Options forfor Saving.
Saving. To-DoApp
To-Do AppDevelopment
Development
Figure 11
Figure 12
17
Lesson # 4
Lesson # 4
Figure 13
18
Creating
Creating Windows
Windows andand Menus,
Menus, Options
Options forfor Saving.
Saving. To-DoApp
To-Do AppDevelopment
Development
# Buttons
button_add_task = Button(frame, text='Add task',
command=add_task)
button_add_task.place(rely=0.15, relwidth=0.25)
19
Lesson # 4
Lesson # 4
Figure 14
Program the Add Task button. After the user clicks on it,
a check will be triggered. The program will ask if the user has
entered anything in the task field. If the answer is yes, a new
task will appear in the field. If the field is empty, an alert will
be displayed (see Figure 15 on page 22).
21
Lesson # 4
Lesson # 4
def add_task():
task = text_input.get()
if task != '':
tasks.append(task)
update_listbox()
else:
messagebox.showwarning('Warning', 'Enter
the task in the input box, please.')
text_input.delete(0, END)
Figure 15
22
Creating
Creating Windows
Windows andand Menus,
Menus, Options
Options forfor Saving.
Saving. To-DoApp
To-Do AppDevelopment
Development
Figure 16
Figure 17
23
Lesson # 4
Lesson # 4
Figure 18
24
Creating
Creating Windows
Windows andand Menus,
Menus, Options
Options forfor Saving.
Saving. To-DoApp
To-Do AppDevelopment
Development
Figure 19
Figure 20
25
Lesson # 4
Lesson # 4
Figure 21
def choose_random():
if len(tasks) > 0:
task = random.choice(tasks)
label_display['text'] = task
else:
messagebox.showwarning('Warning', 'No tasks')
def show_number_of_tasks():
number_of_tasks = len(tasks)
message = 'Number of tasks: %s' % number_of_tasks
label_display['text'] = message
26
Creating
Creating Windows
Windows andand Menus,
Menus, Options
Options forfor Saving.
Saving. To-DoApp
To-Do AppDevelopment
Development
You can use the above code to develop your unique app.
An unusual design and interesting implementation will
only make your To-Do app better.
Look at several solutions offered by other developers.
Habitica is implemented as a game (Figure 22).
Figure 22
Figure 23
27
Lesson # 4
Creating Windows and Menus,
Options for Saving.
To-Do App Development
© STEP IT Academy
www.itstep.org
All rights to protected pictures, audio, and video belong to their authors or legal owners.
Fragments of works are used exclusively in illustration purposes to the extent justified by
the purpose as part of an educational process and for educational purposes in accordance
with Article 1273 Sec. 4 of the Civil Code of the Russian Federation and Articles 21 and 23
of the Law of Ukraine “On Copyright and Related Rights”. The extent and method of cited
works are in conformity with the standards, do not conflict with a normal exploitation of
the work, and do not prejudice the legitimate interests of the authors and rightholders.
Cited fragments of works can be replaced with alternative, non-protected analogs, and
as such correspond the criteria of fair use.
All rights reserved. Any reproduction, in whole or in part, is prohibited. Agreement of the
use of works and their fragments is carried out with the authors and other right owners.
Materials from this document can be used only with resource link.
Liability for unauthorized copying and commercial use of materials is defined according
to the current legislation of Ukraine.