Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.36 KB | None | 0 0
  1. ##Written by Farran Monique Ram
  2. ##fmr0040@arastudent.ac.nz
  3. ##Assessment 3 Part B Question 2
  4. ##create a Speed Analysis application
  5. ##observed speeds are stored in seperate file which application opens & closes
  6.  
  7. from tkinter import *
  8. from tkinter.simpledialog import *
  9. from tkinter.messagebox import *
  10. from tkinter.filedialog import *
  11. import os
  12.  
  13. def get_nearest_speed(recorded_speeds, speed_limit):
  14.     """
  15.    This function takes a list of integers and compares them to see
  16.    which is closest to the given speed limit
  17.    Returns: integer
  18.    """
  19.     list_number = 0
  20.     single_number = 1
  21.     negative_integer = -1
  22.     miniumum_positive_num = 0
  23.     speed_differences = []
  24.  
  25.     for each_speed in recorded_speeds:
  26.         speed_difference = speed_limit - each_speed
  27.         #if negative difference then make net positive for comparison
  28.         if speed_difference < miniumum_positive_num:
  29.             speed_difference = speed_difference*negative_integer
  30.         #put speed differences in a list
  31.         speed_differences.append(speed_difference)
  32.  
  33.     #get minimum difference and its index in recorded_speeds list
  34.     smallest_difference = min(speed_differences)
  35.     closest_speed_index = speed_differences.index(smallest_difference)
  36.  
  37.     return recorded_speeds[closest_speed_index]
  38.  
  39. def display_about():
  40.     """
  41.    Displays an about message box.
  42.    Returns: showinfo dialogue box
  43.    """
  44.     showinfo(title = "Speed Analyser",
  45.                      message = "v1 by Farran Monique Ram")
  46.     return
  47.  
  48. def exit_menu():
  49.     """
  50.    Closes the root window when asked to exit
  51.    returns: none
  52.    """
  53.     root_window.destroy()
  54.     return
  55.  
  56. def get_data():
  57.     """
  58.    This function creates a list from a file then runs the get_nearest_speed function
  59.    Provides a dialogue box message which tells user the closest number to the selected speed limit
  60.    Returns: dialogue message
  61.    """
  62.     speed_differences = []
  63.     selection = option_value.get()
  64.  
  65.     if selection == 0:
  66.         showwarning(title = "Speed analysis.", message = "Please select a speed limit.")
  67.     else:
  68.         name_of_file =  askopenfilename(initialdir = ".", title = "Select file",
  69.                                         filetypes = (("Text files","*.txt"), ("All files","*.*")))
  70.         #open file, add to list then
  71.         the_file = open(name_of_file)
  72.         for each_line in the_file:
  73.             each_line = int(each_line)
  74.             speed_differences.append(each_line)
  75.         the_file.close()
  76.  
  77.         #get closest value to speed limit
  78.         closest_speed = get_nearest_speed(speed_differences, selection)
  79.         showinfo(title = "Speed analysis status.",
  80.                  message = "The nearest recorded speed to {} was {}".format(selection, closest_speed))
  81.  
  82.         #clear selection
  83.         option_value.set(0)
  84.     return
  85.  
  86.  
  87. button_width = 25
  88. root_window = Tk()
  89. root_window.title("Speed analyser")
  90.  
  91. # Set window size and disable resizing in px
  92. root_window.geometry("275x125")
  93. root_window.resizable(width=False, height=False)
  94.  
  95.  
  96. ##add a menu
  97. menubar = Menu(root_window)
  98.  
  99. ##menu option to clear window
  100. file_menu = Menu(menubar, tearoff=0)
  101. file_menu.add_command(label = "Get data", command = get_data)
  102. file_menu.add_separator()
  103.  
  104. ##menu option to exit root menu
  105. file_menu.add_command(label = "Exit", command = exit_menu)
  106. menubar.add_cascade(label = "File", menu = file_menu)
  107.  
  108. ##help follwed by an cascading about option to display help
  109. help_menu = Menu(menubar, tearoff=0)
  110. help_menu.add_command(label = "About", command = display_about)
  111. menubar.add_cascade(label = "Help", menu = help_menu)
  112. root_window.config(menu = menubar)
  113.  
  114.  
  115. ##radio button options
  116. option_value = IntVar()
  117. # Start with no button selected
  118. option_value.set(0)
  119.  
  120. #30km/h button
  121. button1 = Radiobutton(root_window , text="30 km/h", variable = option_value, value = 30)
  122. button1.pack(anchor = W)
  123. #50km/h button
  124. button2 = Radiobutton(root_window , text="50 km/h", variable = option_value, value = 50)
  125. button2.pack(anchor = W)
  126. #70km/h button
  127. button3 = Radiobutton(root_window , text="70 km/h", variable = option_value, value = 70)
  128. button3.pack(anchor = W)
  129.  
  130.  
  131. ##Add a button to root window to confirm radio button selection and start get_data function  
  132. enter_button = Button(None, text = "Get data", command = get_data)
  133. enter_button.pack()
  134.  
  135.  
  136. #do not exit until asked to
  137. root_window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement