AS91896 - Python Report
AS91896 - Python Report
1
Alex Beattie
Contents
The Standard Page 2
Learning Tkinter Page 3
Plan Page 4
Start Page Wireframe Page 5
First Page Page 6
Button Testing Page 8
Item Button (with loops) Page 9
Item Order-Windows Page 13
To-Do List Page 15
Fixing Errors Page 18
Data Entry Screen Page 19
Boundary Testing Page 20
Order Summary Page Page 22
Reformatting Page 23
Pep8 Conventions Page 25
2
Alex Beattie
1. Non-Core Libraries
For my non-core libraries, I am currently using NumPy and Tkinter.
I’ve haven’t spent much time on Tkinter yet so I will focus on NumPy…
NumPy (short for Numerical Python) is an open source library for Python. It’s commonly
used for handling numerical data which will be useful for this standard as there will need to
be ways to work and use the numerical data determined by the user.
3
Alex Beattie
Learning Tkinter
I started learning Tkinter via videos on
Youtube by Edureka!. I just created a basic
GUI by creating a window as shown here.
wanted to make the window run in full-screen so I spent some time trying to allow that to
work as well has have key binds to enter and exit full-screen if the user chooses.
In order to be able to click a button to open new windows (frames), I did the follow so that
when certain buttons are clicked by
the user, a new frame will be opened
and replace the current frame by
destroying the current frame.
4
Alex Beattie
Plan
Home/Start Page:
Buttons to go to the ordering screen (where the menu is)
Buttons where you choose to deliver or pickup (may include these later on
Exit button
Order Page:
Buttons for every item (show price and name)
Item buttons open a window with + and – buttons for adding and removing items
Button to return to start page
Button to continue with order
Display Total
Order Display Page:
Show the total cost
Show all items added to order
Button to return to previous page
Button to Continue with order
Data Entry Page:
Entry Boxes where user can input information for pick-up or delivery (name, address,
phone number etc.)
Button to return to previous page
Button to return home
Button to continue with order
Summary Page:
Display all items added to order
Display total cost
Display personal info (will only show address if delivery is selected)
Message informing the user that the order is being prepared
5
Alex Beattie
6
Alex Beattie
One button is to exit the programme, the other two are for whether the user is ordering with
pick-up or delivery.
This is my test home page. It currently features the 3 buttons as described in my last
comment.
7
Alex Beattie
the formatting for the buttons on the start page to get a basic idea on the page.
My next steps from this point would be to work on the formatting of the buttons to make the
GUI more user friendly and visually appealing and figuring out a different way to include
buttons for each item in a way that means I don’t have to have a line of code for every
individual button. For this I will use “for” loops.
8
Alex Beattie
When trying to allow for buttons on the delivery screen to create a new window (for the input
of the number of that item are being ordered), I had an issue with something inside of the
delivery frame. When on the main menu, I click the “delivery” button, the screen no longer
changes to the delivery frame but remains on the menu frame as well as the error message as
shown above. I am going to attempt to fix the problem by cutting anything that may cause the
issue and pasting it into another python file (in order to keep the code in case it is not the
culprit).
I removed the code shown below and now the delivery frame opens again so I am going to try
and work out how to include what I want to include without breaking my code again.
I will now try and determine which of the shown code is the trouble and find a way to ensure
it doesn’t break my code again.
9
Alex Beattie
I’ve pasted the dictionary “itemdict” back into the original code and it still works so the
problem must lie in either the “orderwindow” or “range”.
The issue I was having was due to the “for i in range” command. It appears like the syntax is
slightly incorrect which causes the keyerror. I haven’t yet worked out how to fix the syntax
but I’ll spend some time doing that now.
This section of code was originally causing an issue in “Key Error 1” but this was fixed by
changing “itemdict[x]” to “itemdict[str(x)]”. Now all of the buttons from the dictionary
The reason the buttons are formatted weirdly is due to “.grid(row = x, column = x)”
10
Alex Beattie
way I formatted this was using “row = 1, 2, 3” and then I played around with “column =
x+int”. It took a bit of time to work out how to get the columns to line up but now my
Delivery screen looks like this:
Before adding the commands to the buttons I want to figure out how to show the price as well
as the name of the item.
I added the prices to the buttons using the same way as adding the item name as they are both
part of the same dictionary. I did “, itemdict[str(x)]” just like before but replaced ‘name’ with
‘price’. I also added a “\n{}” after “text’{}, “ so that the price will be shown underneath the
text which improves looks.
My next steps are to now add functionality to the buttons to make them open new order
windows so you can add that item to the order.
11
Alex Beattie
12
Alex Beattie
As you can see, the small window in the top left has appeared when I click on of the 12 item
buttons. This was done by adding the following to the code for each button:
“command=lambda: master.createorderwindow(),”
Now what I am trying to do is add the functionality of the window (toplevel) by making it so
that you can add that item to order using buttons inside the window.
For this I have created 2 events for the two buttons:
What these events do is increase or decrease the ‘amount’ variable each time their respective
button is pressed (i.e. when you press the + button, it will add 1 more item to the order).
13
Alex Beattie
Since I used events, I must use a bind function to run the events. So I gave each button its
own name then added a bind so that when that button is clicked, it will execute the event.
14
Alex Beattie
‘For example if you wanted to add 3 Beef Kebabs to order you simply click the + button 3
times:
My current issue is that each time one of the buttons are pressed, the following error message
shows in the python shell but I won’t worry about this issue for now as the it will only need
to be fixed when I show the active prices/total. For now I will remove the line
“master.switch_frame(Delivery)” and only try include it once its required.
To-do List
Currently the things I need to do/fix are:
Make the total and subtotal values change when + and – buttons are pressed
Ensure that the maths of the total and subtotal are correct/without issues
Show the correct subtotal inside the item’s toplevel for that specific item
Show the correct total on the ordering frame
Create the logging details for customers’ information
15
Alex Beattie
After spending much time troubleshooting and often replacing code, I managed to make it so
that when items are added to the order (through the + and – buttons), the correct price is
added or subtracted from the total, which is displayed on the bottom left of the ordering
screen.
The “def plus/minus(event):” is simply the command that runs when either the + or – button
is clicked. Here I use toptotal and switchtotal to make it so the total price for that specific
item (this value resets when you add a new item to the order). Then inside the “for x in
range(1,12):” which is used to make it so it changes every time a new item is selected.
An important thing to get the total price to show everytime an item is added to the order was
moving the entire “def orderwindow” code inside the same class as the main ordering page
(the one with all of the buttons). I created a new class within the main class, “class
Delivery(tk.Frame):” so that the orderwindow could call to it so that it could refresh/reload
the frame, thus updating the total so the user can see what the current value is. This reload
command occurs when the user closes the window. I did this using the line
“orderwindow.protocol("WM_DELETE_WINDOW", refresh)”. This just means that when
16
Alex Beattie
the user closes the window, it will run the command “refresh” which is highlighted in red
below.
17
Alex Beattie
I have now created the basic window that shows the user the total price and items they
ordered.
I’ve also noticed an issue with the maths for the total price, in the screenshot to the right, all
of the items added cost $8.50 so the total should equal 3 * 8.5 which equals $25.50, not $34
so I will sort that out before I continue with finishing this window or the page where the user
inputs the required info.
Another issue I’ve only just noticed is that only 9 of the 12 menu items are showing up on the
main ordering window. Every 4th one just doesn’t display so I will have a play around with
changing values for my buttons (i.e. padding, sizes and the for loop) to see what is causing
the problem.
Fixing Errors
I deleted all of my code involving the total price of items as there were so many issues it is
easier just to restart. I used a for loop so that I can multiply the price of each individual item
with its respective price so that the correct price will be added or subtracted from the total.
For example, when someone adds 2 “Beef Kebabs” to the order, the maths behind what it will
add to the total is the amount * price. So since the amount = 2 and the price is $8.50, the
added price to the total will = 2 * $8.50 = $17
18
Alex Beattie
I realized the issue with my buttons that weren’t displaying was in my for loop command.
Every 4th item wasn’t showing due to my “for x in range()” not including the every 4 th item. I
used the ranges 1-4, 5-8 and 9-12 but what I needed to change it to was 1-5, 5-9, 9-13. As the
range only includes the integers within the range meaning the highest number in the range
isn’t included.
As you can see above, all 12 of my items display correctly meaning that this part of the
assessment is met as 12 items are required to pass.
19
Alex Beattie
time trying to format each individual item. Tk.Label was simply used to title each entry box
(for the user). To connect the tk.Label, tk.Entry and tk.Frame, I used tk.BOTH as it allows
Boundary Testing
One of the requirements for the standard is that input data is checked for validity. I.E. the user
doesn’t input negative amounts of items or a phone number that includes letters. Doing this
ensures no inputs are made which break or cause errors in the code, causing unnecessary
issues for the user.
The first Error I will address is the number of items that can be added to the order. The
maximum of an item the user can add is 5 and the minimum is of course 0 as you can’t order
negative items. For this I will create a simple if/else command that checks if something is
greater than 5 or less than 0 and makes it = the respective minimum or maximum depending
on the situation. Here, I also think a visual error message for the user would be a good choice
as it means the user doesn’t get left unsure why they can’t include more than 5.
20
Alex Beattie
Now I will address my issue with phone numbers/names. I think a valid requirement for a
name input should be all letters. This eliminates the
possibility for there to be names with non-alphabetic
characters such as numbers or symbols.
For this I created a new list with the entries from the
user so that they can be
referred to in other lines of code as a variable, making things
easier to manage.
I then created a variable “errors” this just means that only one
error window can show at one time if there are multiple issues
present (i.e. both mistakes in the phone number and name).
21
Alex Beattie
All of the error checking is done within the “def errortest():” as seen to the left. This means
that the whole function can be called on as one big group which again, simplifies things for
me.
22
Alex Beattie
“if info[0].isalpha():” simply means if info[0] (which is the name the user inputted into the
entry) only contains letters from the alphabet, it will not run the error code. If this is not the
case, and since “errors” already equals 0, the error code will run displaying the message as a
TopLevel.
items ordered, the total cost and whether it was being delivered or picked up.
For my if/elif/else, I included an error page for the “else” as when I was first testing having
different messages depending on whether the user chose to pickup or have it delivered, I was
having difficulty getting it to work meaning that the “else” part of it would run. Because of
23
Alex Beattie
this, I created essentially an error page incase the user comes across an issue (that I am yet to
find,) which would affect the if/else code.
Reformatting
The main page where I felt the formatting could be improved is my order display page. When
testing my code, I realized on this page that the formatting had a couple of issues which
became worse when I wanted to include on the delivery button, “($3 charge)” as including a
delivery cost of $3 was a requirement. I needed
to change the
sizing of the
buttons and
their text to
make it fit as
well as I wanted
to move the
item + amount
info to the right.
(before on left,
after on right)
24
Alex Beattie
There was still an issue where everything is very close to the left side of the screen with the
whole right side being blank. To fix this I simply added a tk.Label with its text being a couple
spaces. Then I tinkered with its “padx” value until the rest of the things were centered on the
screen. If I had more time to finish this, I would have used a more elegant solution but this
will suffice for its purpose. (below is full-screen screenshot)
25
Alex Beattie
Pep8 Conventions
Pep8 conventions are simply the way that we format python code. Examples of things where
pep8 applies are variables, commands and classes.
This means using the same type of formatting throughout. For example, only using
apostrophes rather than sometimes
switching between them and
quotation marks.
As you can see, all of the variables
require quotation marks, so I’ve used
apostrophes to do this. Throughout
I’ve decided to only use apostrophes
as quote marks to follow the string
quotes pep8 convention.
One thing to note is “Hammond’s Salmon” and “May’s Earl Gray” uses double quotes as
there is an apostrophe that needs to be displayed in the text. Here pep8 dictates that double
example:
and
For defining things, pep8 dictates that I must use lowercase letters and underscores to
separate words. Examples:
and
26