Advertisement
Guest User

Untitled

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