local.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import os
  2. import os.path
  3. import tkinter as tk
  4. from tkinter import ttk, filedialog, messagebox
  5. from utils.config import config
  6. from utils.tools import resource_path
  7. class LocalUI:
  8. def init_ui(self, root):
  9. """
  10. Init local UI
  11. """
  12. frame_local_open_local = tk.Frame(root)
  13. frame_local_open_local.pack(fill=tk.X)
  14. self.open_local_label = tk.Label(
  15. frame_local_open_local, text="开启本地源:", width=8
  16. )
  17. self.open_local_label.pack(side=tk.LEFT, padx=4, pady=8)
  18. self.open_local_var = tk.BooleanVar(value=config.open_local)
  19. self.open_local_checkbutton = ttk.Checkbutton(
  20. frame_local_open_local,
  21. variable=self.open_local_var,
  22. onvalue=True,
  23. offvalue=False,
  24. command=self.update_open_local,
  25. )
  26. self.open_local_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  27. frame_local_file = tk.Frame(root)
  28. frame_local_file.pack(fill=tk.X)
  29. frame_local_file_column1 = tk.Frame(frame_local_file)
  30. frame_local_file_column1.pack(side=tk.LEFT, fill=tk.Y)
  31. frame_local_file_column2 = tk.Frame(frame_local_file)
  32. frame_local_file_column2.pack(side=tk.LEFT, fill=tk.Y)
  33. self.local_file_label = tk.Label(
  34. frame_local_file_column1, text="本地源文件:", width=8
  35. )
  36. self.local_file_entry = tk.Entry(frame_local_file_column1)
  37. self.local_file_label.pack(side=tk.LEFT, padx=4, pady=8)
  38. self.local_file_entry.pack(fill=tk.X, padx=4, expand=True)
  39. self.local_file_entry.insert(0, config.local_file)
  40. self.local_file_button = tk.ttk.Button(
  41. frame_local_file_column2,
  42. text="选择文件",
  43. command=self.select_local_file,
  44. )
  45. self.local_file_button.pack(side=tk.LEFT, padx=4, pady=0)
  46. self.local_file_edit_button = tk.ttk.Button(
  47. frame_local_file_column2,
  48. text="编辑",
  49. command=lambda: self.edit_file(config.local_file),
  50. )
  51. self.local_file_edit_button.pack(side=tk.LEFT, padx=4, pady=0)
  52. def update_open_local(self):
  53. config.set("Settings", "open_local", str(self.open_local_var.get()))
  54. def select_local_file(self):
  55. filepath = filedialog.askopenfilename(
  56. initialdir=os.getcwd(), title="选择本地源文件", filetypes=[("txt", "*.txt")]
  57. )
  58. if filepath:
  59. self.local_file_entry.delete(0, tk.END)
  60. self.local_file_entry.insert(0, filepath)
  61. config.set("Settings", "local_file", filepath)
  62. def edit_file(self, path):
  63. if os.path.exists(resource_path(path)):
  64. os.system(f'notepad.exe {path}')
  65. else:
  66. print(f"File {path} not found!")
  67. messagebox.showerror("Error", f"File {path} not found!")
  68. def change_entry_state(self, state):
  69. for entry in [
  70. "open_local_checkbutton",
  71. "local_file_entry",
  72. "local_file_button",
  73. "local_file_edit_button",
  74. ]:
  75. getattr(self, entry).config(state=state)