Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##Written by Farran Monique Ram
- ##fmr0040@arastudent.ac.nz
- ##Assessment 3 Part B Question 2
- ##create a Speed Analysis application
- ##observed speeds are stored in seperate file which application opens & closes
- from tkinter import *
- from tkinter.simpledialog import *
- from tkinter.messagebox import *
- from tkinter.filedialog import *
- import os
- def get_nearest_speed(recorded_speeds, speed_limit):
- """
- This function takes a list of integers and compares them to see
- which is closest to the given speed limit
- Returns: integer
- """
- list_number = 0
- single_number = 1
- negative_integer = -1
- miniumum_positive_num = 0
- speed_differences = []
- for each_speed in recorded_speeds:
- speed_difference = speed_limit - each_speed
- #if negative difference then make net positive for comparison
- if speed_difference < miniumum_positive_num:
- speed_difference = speed_difference*negative_integer
- #put speed differences in a list
- speed_differences.append(speed_difference)
- #get minimum difference and its index in recorded_speeds list
- smallest_difference = min(speed_differences)
- closest_speed_index = speed_differences.index(smallest_difference)
- return recorded_speeds[closest_speed_index]
- def display_about():
- """
- Displays an about message box.
- Returns: showinfo dialogue box
- """
- showinfo(title = "Speed Analyser",
- message = "v1 by Farran Monique Ram")
- return
- def exit_menu():
- """
- Closes the root window when asked to exit
- returns: none
- """
- root_window.destroy()
- return
- def get_data():
- """
- This function creates a list from a file then runs the get_nearest_speed function
- Provides a dialogue box message which tells user the closest number to the selected speed limit
- Returns: dialogue message
- """
- speed_differences = []
- selection = option_value.get()
- if selection == 0:
- showwarning(title = "Speed analysis.", message = "Please select a speed limit.")
- else:
- name_of_file = askopenfilename(initialdir = ".", title = "Select file",
- filetypes = (("Text files","*.txt"), ("All files","*.*")))
- #open file, add to list then
- the_file = open(name_of_file)
- for each_line in the_file:
- each_line = int(each_line)
- speed_differences.append(each_line)
- the_file.close()
- #get closest value to speed limit
- closest_speed = get_nearest_speed(speed_differences, selection)
- showinfo(title = "Speed analysis status.",
- message = "The nearest recorded speed to {} was {}".format(selection, closest_speed))
- #clear selection
- option_value.set(0)
- return
- button_width = 25
- root_window = Tk()
- root_window.title("Speed analyser")
- # Set window size and disable resizing in px
- root_window.geometry("275x125")
- root_window.resizable(width=False, height=False)
- ##add a menu
- menubar = Menu(root_window)
- ##menu option to clear window
- file_menu = Menu(menubar, tearoff=0)
- file_menu.add_command(label = "Get data", command = get_data)
- file_menu.add_separator()
- ##menu option to exit root menu
- file_menu.add_command(label = "Exit", command = exit_menu)
- menubar.add_cascade(label = "File", menu = file_menu)
- ##help follwed by an cascading about option to display help
- help_menu = Menu(menubar, tearoff=0)
- help_menu.add_command(label = "About", command = display_about)
- menubar.add_cascade(label = "Help", menu = help_menu)
- root_window.config(menu = menubar)
- ##radio button options
- option_value = IntVar()
- # Start with no button selected
- option_value.set(0)
- #30km/h button
- button1 = Radiobutton(root_window , text="30 km/h", variable = option_value, value = 30)
- button1.pack(anchor = W)
- #50km/h button
- button2 = Radiobutton(root_window , text="50 km/h", variable = option_value, value = 50)
- button2.pack(anchor = W)
- #70km/h button
- button3 = Radiobutton(root_window , text="70 km/h", variable = option_value, value = 70)
- button3.pack(anchor = W)
- ##Add a button to root window to confirm radio button selection and start get_data function
- enter_button = Button(None, text = "Get data", command = get_data)
- enter_button.pack()
- #do not exit until asked to
- root_window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement