spacepaste

  1.  
  2. import sqlite3 as sqlite
  3. import tkinter as tk
  4. from tkinter import ttk
  5. #GUI Widgets
  6. class EsperantoDict:
  7. def __init__(self, master):
  8. master.title("EsperantoDict")
  9. master.iconbitmap("Esperanto.ico")
  10. master.resizable(False, False)
  11. master.configure(background='#EAFFCD')
  12. self.style = ttk.Style()
  13. self.search_var = tk.StringVar()
  14. self.search_var.trace("w", lambda name, index, mode: self.update_list())
  15. self.style = ttk.Style()
  16. self.style.configure("TFrame", background='#EAFFCD')
  17. self.style.configure("TButton", background='#C6FF02')
  18. self.style.configure("TLabel", background='#EAFFCD')
  19. self.frame_header = ttk.Frame(master, relief=tk.FLAT)
  20. self.frame_header.config(style="TFrame")
  21. self.frame_header.pack(side=tk.TOP, padx=5, pady=5)
  22. self.logo = tk.PhotoImage(file=r'C:\EsperantoDict\eo.png')
  23. self.small_logo = self.logo.subsample(10, 10)
  24. ttk.Label(self.frame_header, image=self.small_logo).grid(row=0, column=0, stick="ne", padx=5, pady=5, rowspan=2)
  25. ttk.Label(self.frame_header, text='EsperantoDict', font=('Arial', 18, 'bold')).grid(row=0, column=1)
  26. self.frame_content = ttk.Frame(master)
  27. self.frame_content.config(style="TFrame")
  28. self.frame_content.pack()
  29. self.entry_search = ttk.Entry(self.frame_content, textvariable=self.search_var, width=30)
  30. self.entry_search.bind('<FocusIn>', self.entry_delete)
  31. self.entry_search.bind('<FocusOut>', self.entry_insert)
  32. self.entry_search.grid(row=0, column=0, padx=5)
  33. self.entry_search.focus()
  34. self.entry_search.bind("<Key>", self.edit_input)
  35. self.button_search = ttk.Button(self.frame_content, text="Search")
  36. self.photo_search = tk.PhotoImage(file=r'C:\EsperantoDict\search.png')
  37. self.small_photo_search = self.photo_search.subsample(3, 3)
  38. self.button_search.config(image=self.small_photo_search, compound=tk.LEFT, style="TButton")
  39. self.button_search.grid(row=0, column=2, columnspan=1, sticky='nw', padx=5)
  40. self.listbox = tk.Listbox(self.frame_content, height=30, width=30)
  41. self.listbox.grid(row=1, column=0, padx=5)
  42. self.scrollbar = ttk.Scrollbar(self.frame_content, orient=tk.VERTICAL, command=self.listbox.yview)
  43. self.scrollbar.grid(row=1, column=1, sticky='nsw')
  44. self.listbox.config(yscrollcommand=self.scrollbar.set)
  45. self.listbox.bind('<<ListboxSelect>>', self.enter_meaning)
  46. self.textbox = tk.Text(self.frame_content, relief=tk.GROOVE, width=60, height=30, borderwidth=2)
  47. self.textbox.config(wrap='word')
  48. self.textbox.grid(row=1, column=2, sticky='w', padx=5)
  49. # SQLite
  50. self.db = sqlite.connect(r'C:\EsperantoDict\test.db')
  51. self.cur = self.db.cursor()
  52. self.cur.execute('SELECT Esperanto FROM Words ORDER BY Esperanto')
  53. for row in self.cur:
  54. self.listbox.insert(tk.END, row)
  55. for row in range(0, self.listbox.size(), 2):
  56. self.listbox.itemconfigure(row, background="#f0f0ff")
  57. self.update_list()
  58. def update_list(self):
  59. search_term = self.search_var.get()
  60. for item in self.listbox.get(0, tk.END):
  61. if search_term.lower() in item:
  62. self.listbox.delete(0, tk.END)
  63. self.listbox.insert(tk.END, item)
  64. def edit_input(self, tag):
  65. words = ["ĝ", "ĉ", "ĥ", "ĵ", "ŭ", "ŝ"]
  66. char = ["gx", "cx", "hx", "jx", "ux", "sx"]
  67. result = ''
  68. if True:
  69. user_in = self.search_var.get().lower()
  70. if user_in in char:
  71. if char[0] in user_in:
  72. result = words[0]
  73. elif char[1] in user_in:
  74. result = words[1]
  75. elif char[2] in user_in:
  76. result = words[2]
  77. elif char[3] in user_in:
  78. result = words[3]
  79. elif char[4] in user_in:
  80. result = words[4]
  81. elif char[5] in user_in:
  82. result = words[5]
  83. return self.entry_search.insert(tk.END, result)
  84. # SQLite
  85. def enter_meaning(self, tag):
  86. for index in self.listbox.curselection():
  87. esperanto = self.listbox.get(index)
  88. results = self.cur.execute("SELECT English FROM Words WHERE Esperanto = ?", esperanto)
  89. for row in results:
  90. self.textbox.delete(1.0, tk.END)
  91. self.textbox.insert(tk.END, row)
  92. def entry_delete(self, tag):
  93. self.entry_search.delete(0, tk.END)
  94. return None
  95. def entry_insert(self, tag):
  96. self.entry_search.delete(0, tk.END)
  97. self.entry_search.insert(0, "Type to Search")
  98. return None
  99. def main():
  100. root = tk.Tk()
  101. esperantodict = EsperantoDict(root)
  102. root.mainloop()
  103. if __name__ == '__main__': main()
  104.