Advertisement
Guest User

Untitled

a guest
Dec 7th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. import tkinter as tk
  2. import mysql.connector
  3.  
  4. # Connect to database
  5. database = mysql.connector.connect(
  6. host="localhost",
  7. user="root",
  8. password="YaNa2203",
  9. )
  10.  
  11. # Create Window
  12. window = tk.Tk()
  13. window.title("Service Finder")
  14.  
  15. # Window properties
  16. window.resizable(width=False, height=False)
  17.  
  18. ## Widgets
  19.  
  20. # Radio options
  21. gpInt = tk.IntVar()
  22. radioGP = tk.Checkbutton(window, text="Doctors", state='disabled', variable=gpInt)
  23. radioGP.grid(row=3, column=0, sticky=tk.W)
  24.  
  25. dentistInt = tk.IntVar()
  26. radioDentist = tk.Checkbutton(window, text="Dentist", state='disabled', variable=dentistInt)
  27. radioDentist.grid(row=4, column=0, sticky=tk.W)
  28.  
  29. opticianInt = tk.IntVar()
  30. radioOptician = tk.Checkbutton(window, text="Optician", state='disabled', variable=opticianInt)
  31. radioOptician.grid(row=5, column=0, sticky=tk.W)
  32.  
  33. schoolInt = tk.IntVar()
  34. radioSchool = tk.Checkbutton(window, text="School", state='disabled', variable=schoolInt)
  35. radioSchool.grid(row=6, column=0, sticky=tk.W)
  36.  
  37. nurseryInt = tk.IntVar()
  38. radioNursery = tk.Checkbutton(window, text="Nursery", state='disabled', variable=nurseryInt)
  39. radioNursery.grid(row=7, column=0, sticky=tk.W)
  40.  
  41. # Search
  42. def loadDatabase():
  43. # Get script
  44. fd = open('Find Distance.sql', 'r')
  45. sqlFile = fd.read()
  46. fd.close()
  47.  
  48. cursor = database.cursor(buffered=True)
  49.  
  50. # Set the correct database
  51. database.cursor().execute("USE project;")
  52.  
  53. text = postcodeField.get()
  54. text = text.replace(" ", "")
  55. text = text[:4] + " " + text[4:]
  56. print(text)
  57. # Replace the constants in the script
  58. sqlFile = sqlFile.replace('$POSTCODE$', text)
  59. sqlFile = sqlFile.replace('$CATEGORY1$', str(gpInt))
  60. sqlFile = sqlFile.replace('$CATEGORY2$', str(dentistInt))
  61. sqlFile = sqlFile.replace('$CATEGORY3$', str(opticianInt))
  62. sqlFile = sqlFile.replace('$CATEGORY4$', str(schoolInt))
  63. sqlFile = sqlFile.replace('$CATEGORY5$', str(nurseryInt))
  64.  
  65. # Execute the script
  66. result = cursor.execute(sqlFile)
  67.  
  68. # Clear list
  69. listbox1.delete(0,tk.END)
  70. listbox2.delete(0,tk.END)
  71. listbox3.delete(0,tk.END)
  72. listbox4.delete(0,tk.END)
  73.  
  74. # Insert database into list
  75. for x in cursor:
  76. listbox1.insert(tk.END, x[0])
  77. listbox2.insert(tk.END, x[1])
  78. listbox3.insert(tk.END, x[2])
  79. listbox4.insert(tk.END, str(x[3]) + " miles")
  80.  
  81. noticeText = tk.StringVar()
  82. noticeLabel = tk.Label(window, textvariable=noticeText)
  83. noticeLabel.grid(row=8, column=0, sticky=tk.W)
  84. noticeText.set("* required field")
  85. # Search button
  86. searchButton = tk.Button(text="Search", command=loadDatabase, state='disabled')
  87. searchButton.grid(row=9, column =0, columnspan=3, sticky=tk.W)
  88.  
  89. # Postcode
  90. postcodeText = tk.StringVar()
  91. postcodeLabel = tk.Label(window, textvariable=postcodeText)
  92. postcodeLabel.grid(row=1, column=0, sticky=tk.W)
  93. postcodeText.set("Postcode *:")
  94.  
  95. # When the postcode text changes
  96. def postChange(text):
  97. text = text.replace(" ", "")
  98. if len(text) != 7:
  99. searchButton.config(state='disabled')
  100. else:
  101. searchButton.config(state='normal')
  102.  
  103. # Postcode Button
  104. postcodeFieldText = tk.StringVar()
  105. postcodeFieldText.trace("w", lambda name, index, mode, postcodeFieldText=postcodeFieldText: postChange(postcodeFieldText.get()))
  106. postcodeField = tk.Entry(window, bd = 5, textvariable=postcodeFieldText)
  107. postcodeField.grid(row=1, column=1, sticky=tk.W)
  108.  
  109. # On age change
  110. def ageChange(text):
  111. # Check the age, and enable the appropriate buttons
  112. if text != "":
  113. radioGP.config(state='normal')
  114. radioDentist.config(state='normal')
  115. radioOptician.config(state='normal')
  116. if int(text) < 4:
  117. radioNursery.config(state='normal')
  118. radioSchool.config(state='normal')
  119. radioSchool.config(state='disabled')
  120. elif int(text) <= 16:
  121. radioNursery.config(state='disabled')
  122. radioSchool.config(state='normal')
  123. else:
  124. radioSchool.config(state='disabled')
  125. radioNursery.config(state='disabled')
  126. var.set(99)
  127. else:
  128. radioSchool.config(state='disabled')
  129. radioNursery.config(state='disabled')
  130. radioGP.config(state='disabled')
  131. radioDentist.config(state='disabled')
  132. radioOptician.config(state='disabled')
  133. var.set(99)
  134.  
  135. ageText = tk.StringVar()
  136. ageLabel = tk.Label(window, textvariable=ageText)
  137. ageLabel.grid(row=2, column=0, sticky=tk.W)
  138. ageText.set("Age *:")
  139.  
  140. ageFieldText = tk.StringVar()
  141. ageFieldText.trace("w", lambda name, index, mode, ageFieldText=ageFieldText: ageChange(ageFieldText.get()))
  142. ageField = tk.Entry(window, bd = 5, textvariable=ageFieldText)
  143. ageField.grid(row=2, column=1, sticky=tk.W)
  144.  
  145. # Canvas for the listboxes
  146. frame = tk.Frame(window, width=400, height=400)
  147. frame.grid(row=0, column=3, rowspan=20)
  148.  
  149. canvas = tk.Canvas(frame, width=400, height=400, scrollregion=(0,0,500,500), confine=False)
  150.  
  151. canvas.pack(side=tk.LEFT,expand=True,fill=tk.BOTH)
  152.  
  153. # SQL table
  154. listbox1 = tk.Listbox(canvas, height=32)
  155. listbox1.grid(column=0, row = 0)
  156. listbox2 = tk.Listbox(canvas, height=32)
  157. listbox2.grid(column=1, row = 0)
  158. listbox3 = tk.Listbox(canvas, height=32)
  159. listbox3.grid(column=2, row = 0)
  160. listbox4 = tk.Listbox(canvas, height=32)
  161. listbox4.grid(column=3, row = 0)
  162.  
  163. # Select the corresponding box on click
  164. def lb1Select(e):
  165. if len(listbox1.curselection()):
  166. listbox2.select_set(listbox1.curselection()[0])
  167. listbox1.bind('<<ListboxSelect>>', lb1Select)
  168.  
  169. # Start window
  170. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement