Guest User

Untitled

a guest
May 16th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | None | 0 0
  1. __Version__ = 1.0
  2. #Imports tkinter module for the GUI
  3. from tkinter import *
  4.  
  5. #creates the initial GUI
  6. root = Tk()
  7. root.title('Converter')
  8. #root.iconbitmap(default='pennicon.ico')
  9. Label(text = 'Penn United Converter', bg='white', fg='blue').grid(row=1, column=1)
  10. Label(text = ' ').grid(row=2)
  11. #Creates the Label next to the entry box to let the  user know what to enter
  12. Label(text = 'Units in MM', bg = "white", fg='blue' ).grid(row=3)
  13. #Function for clearing the entry fields
  14.  
  15. def clear(entry):
  16.     entry.delete(0, END)
  17.    
  18. #Function to convert MM to inches  
  19. def mm_to_inch(amt_in_mm):
  20.     mm_per_inch = 25.4
  21.     return amt_in_mm / mm_per_inch
  22.    
  23. #Function to count the number of characters after the decimal place
  24. def digits_after_decimal_point(number):
  25.     try:
  26.         return (len(number.split('.')[1].strip()))
  27.     except IndexError:
  28.         return 0
  29.        
  30. #Function for taking user input and converting it  
  31. def convert_user_input(event):
  32.     user_input = c.get()
  33.     try:#Checks for a ValueError
  34.         amt_in_mm = float(user_input)
  35.     except ValueError:#If ValueError found clears the entry box and returns to main
  36.         clear(c)
  37.         return
  38.     amt_in_inches = mm_to_inch(amt_in_mm)
  39.     decimal_count = (digits_after_decimal_point(user_input))
  40.     if decimal_count <= 2:#checks decimal count to see what place the number needs taken out to
  41.         amt_in_inches = ("{0:.3f}".format(amt_in_inches))
  42.     else:
  43.         amt_in_inches = ("{0:.4f}".format(amt_in_inches))
  44.     v.set(amt_in_inches)
  45.     root.clipboard_clear()
  46.     root.clipboard_append(amt_in_inches)
  47.     clear(c)
  48. #Entry box for conversion  
  49. c = Entry(root, width = 10)
  50. c.bind('<Return>', convert_user_input)
  51. c.grid(row=3, column=1)
  52. v = StringVar()
  53. Label(textvariable = v, width = 10).grid(row=4, column=1)
  54. #Label to let user know where to enter Surface input
  55. Label(text = 'RA Finish(MM)', bg = "white", fg='blue').grid(row=5)
  56.  
  57. #Function for converting RA Finishes
  58. def surface(RA):
  59.     try:#Trys for value error and defines variables
  60.         float_roughness = float(RA)
  61.         finish = (float_roughness / 25.4) * 1000
  62.         results_finish = "{0:.0f}".format(finish)
  63.     except ValueError:
  64.         clear(s)
  65.         return
  66.     root.clipboard_clear()
  67.     root.clipboard_append(results_finish)  
  68.     var.set(results_finish)
  69.     clear(s)
  70. #Takes input for RA surface finsih conversion  
  71. def surface_user_input(event):
  72.     roughness = s.get()
  73.     surface(roughness)
  74. #Entry box for RA Finish input
  75. s = Entry(root, width = 10)
  76. s.bind('<Return>', surface_user_input)
  77. var = StringVar()
  78. s.grid(row = 5, column = 1)
  79. Label(textvariable=var, width = 10).grid(row=6, column=1)
  80. #Label for Rz Finish input
  81. Label(text = 'Rz Finish', bg='white', fg='blue').grid(row=7, column=0)
  82. #Dictionary for Rz finish's
  83. Rz_dict = { '1' : '4', '2' : '8', '3' : '16', '4' : '16', '5' : '16',
  84. '6' : '32', '7' : '32', '8' : '32', '10' : '32', '16' : '64', '25' : '125',
  85. '32' : '125', '63' : '250'
  86. }
  87.  
  88. #Function to get the correct Rz Value
  89. def Rz_Finish(event):
  90.     Rz = str(r.get())
  91.     Rz_RA = Rz_dict.get(Rz)
  92.     Rz_var.set(Rz_RA)
  93.     clear(r)
  94. #Rz finish input box
  95. r = Entry(root, width = 10)
  96. r.bind('<Return>',Rz_Finish)
  97. Rz_var = StringVar()
  98. r.grid(row=7, column=1)
  99. Label(textvariable=Rz_var, width = 20).grid(row=8, column=1)
  100. #Label for N type finish
  101. Label(text='N Type Finish', bg='white', fg='blue').grid(row=9, column=0)
  102. #N type dictionary
  103. N_type_dict = { '1' : '1', '2' : '2', '3' : '4', '4' : '8', '5' : '16','6' : '32',
  104. '7' : '63', '8' : '125', '9' : '250', '10' : '500', '11' : '1000', '12' : '2000'
  105. }
  106.  
  107. def N_type_finish(event):
  108.     N_type = str(n.get())
  109.     N_converted = N_type_dict.get(N_type)
  110.     N_var.set(N_converted)
  111.     clear(n)
  112. #Entry box for N Type Finish
  113. n = Entry(root, width =10)
  114. n.bind('<Return>',N_type_finish)
  115. N_var = StringVar()
  116. n.grid(row=9, column=1)
  117. Label(textvariable=N_var, width=10).grid(row=10, column=1) 
  118.  
  119. #Function for the close button
  120. def quit():
  121.     global root
  122.     root.destroy() 
  123.    
  124.  
  125. root.mainloop()
  126.    
  127. Button(root, text='Close', command=quit).grid(row = 11, column = 4)
  128.  
  129. main()
Advertisement
Add Comment
Please, Sign In to add comment