prefer.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import tkinter as tk
  2. from tkinter import ttk
  3. from utils.config import config
  4. class PreferUI:
  5. def init_ui(self, root=None):
  6. """
  7. Init prefer UI
  8. """
  9. config_options = [
  10. {"label_text": f"结果来源优先{i + 1}:", "combo_box_value": value}
  11. for i, value in enumerate(self.get_origin_type_prefer_index(config.origin_type_prefer))
  12. ]
  13. self.origin_type_prefer_options = []
  14. for config_option in config_options:
  15. option = ConfigOption(root, **config_option)
  16. option.combo_box.bind(
  17. "<<ComboboxSelected>>",
  18. option.update_select,
  19. )
  20. option.entry.bind("<KeyRelease>", option.update_input)
  21. self.origin_type_prefer_options.append(option)
  22. frame_prefer_ipv_type = tk.Frame(root)
  23. frame_prefer_ipv_type.pack(fill=tk.X)
  24. self.prefer_ipv_type_label = tk.Label(
  25. frame_prefer_ipv_type, text="结果协议优先:", width=12
  26. )
  27. self.prefer_ipv_type_label.pack(side=tk.LEFT, padx=4, pady=8)
  28. self.prefer_ipv_type_combo = ttk.Combobox(frame_prefer_ipv_type)
  29. self.prefer_ipv_type_combo.pack(side=tk.LEFT, padx=4, pady=8)
  30. self.prefer_ipv_type_combo["values"] = ("IPv4", "IPv6", "自动")
  31. ipv_type_prefer = config.ipv_type_prefer
  32. if ipv_type_prefer[0] == "ipv4":
  33. self.prefer_ipv_type_combo.current(0)
  34. elif ipv_type_prefer[0] == "ipv6":
  35. self.prefer_ipv_type_combo.current(1)
  36. else:
  37. self.prefer_ipv_type_combo.current(2)
  38. self.prefer_ipv_type_combo.bind(
  39. "<<ComboboxSelected>>", self.update_ipv_type_prefer
  40. )
  41. self.ipv_type_input = []
  42. for ipv_type in ["ipv4", "ipv6"]:
  43. input = IpvNumInput(root, ipv_type)
  44. input.entry.bind("<KeyRelease>", input.update_input)
  45. self.ipv_type_input.append(input)
  46. frame_prefer_open_supply = tk.Frame(root)
  47. frame_prefer_open_supply.pack(fill=tk.X)
  48. self.open_supply_label = tk.Label(
  49. frame_prefer_open_supply, text="开启补偿模式:", width=12
  50. )
  51. self.open_supply_label.pack(side=tk.LEFT, padx=4, pady=8)
  52. self.open_supply_var = tk.BooleanVar(value=config.open_supply)
  53. self.open_supply_checkbutton = ttk.Checkbutton(
  54. frame_prefer_open_supply,
  55. variable=self.open_supply_var,
  56. onvalue=True,
  57. offvalue=False,
  58. command=self.update_open_supply,
  59. )
  60. self.open_supply_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)
  61. def get_origin_type_prefer_index(self, origin_type_prefer):
  62. index_list = [None, None, None, None]
  63. origin_type_prefer_obj = {
  64. "hotel": 0,
  65. "multicast": 1,
  66. "subscribe": 2,
  67. "online_search": 3,
  68. }
  69. for i, item in enumerate(origin_type_prefer):
  70. index_list[i] = origin_type_prefer_obj[item]
  71. return index_list
  72. def update_ipv_type_prefer(self, event):
  73. config.set(
  74. "Settings",
  75. "ipv_type_prefer",
  76. self.prefer_ipv_type_combo.get(),
  77. )
  78. def update_open_supply(self):
  79. config.set("Settings", "open_supply", str(self.open_supply_var.get()))
  80. def change_entry_state(self, state):
  81. for option in self.origin_type_prefer_options:
  82. option.change_state(state)
  83. self.prefer_ipv_type_combo.config(state=state)
  84. for input in self.ipv_type_input:
  85. input.change_state(state)
  86. self.open_supply_checkbutton.config(state=state)
  87. class IpvNumInput:
  88. def __init__(self, master, ipv_type):
  89. self.master = master
  90. self.ipv_type = ipv_type
  91. self.frame = tk.Frame(master)
  92. self.frame.pack(fill=tk.X)
  93. self.frame_column1 = tk.Frame(self.frame)
  94. self.frame_column1.pack(side=tk.LEFT, fill=tk.Y)
  95. self.frame_column2 = tk.Frame(self.frame)
  96. self.frame_column2.pack(side=tk.RIGHT, fill=tk.Y)
  97. ipv_type_text = "IPv4" if ipv_type == "ipv4" else "IPv6"
  98. self.entry_label = tk.Label(
  99. self.frame_column1, text=f"{ipv_type_text}数量:", width=12
  100. )
  101. self.entry_label.pack(side=tk.LEFT, padx=4, pady=8)
  102. self.entry = tk.Entry(self.frame_column1)
  103. self.entry.insert(0, config.ipv_limit[ipv_type])
  104. self.entry.pack(side=tk.LEFT, padx=4, pady=8)
  105. def update_input(self, event):
  106. config.set(
  107. "Settings",
  108. f"{self.ipv_type}_num",
  109. self.entry.get(),
  110. )
  111. def change_state(self, state):
  112. self.entry.config(state=state)
  113. class ConfigOption:
  114. def __init__(self, master, label_text, combo_box_value):
  115. self.master = master
  116. self.label_text = label_text
  117. self.combo_box_value = combo_box_value
  118. self.frame = tk.Frame(master)
  119. self.frame.pack(fill=tk.X)
  120. self.column1 = tk.Frame(self.frame)
  121. self.column1.pack(side=tk.LEFT, fill=tk.Y)
  122. self.column2 = tk.Frame(self.frame)
  123. self.column2.pack(side=tk.RIGHT, fill=tk.Y)
  124. self.label = tk.Label(self.column1, text=label_text, width=12)
  125. self.label.pack(side=tk.LEFT, padx=4, pady=8)
  126. self.combo_box = ttk.Combobox(self.column1)
  127. self.origin_type_prefer_obj = {
  128. "酒店源": "hotel",
  129. "组播源": "multicast",
  130. "订阅源": "subscribe",
  131. "关键字搜索": "online_search",
  132. }
  133. combo_box_values_name = list(self.origin_type_prefer_obj.keys())
  134. self.combo_box["values"] = combo_box_values_name
  135. self.combo_box.pack(side=tk.LEFT, padx=4, pady=8)
  136. origin_type_prefer = config.origin_type_prefer
  137. if not origin_type_prefer or combo_box_value is None:
  138. self.combo_box.current(None)
  139. else:
  140. self.combo_box.current(combo_box_value)
  141. self.entry_label = tk.Label(self.column2, text="数量:", width=12)
  142. self.entry_label.pack(side=tk.LEFT, padx=4, pady=8)
  143. self.entry = tk.Entry(self.column2)
  144. if origin_type_prefer and combo_box_value is not None:
  145. self.entry.insert(
  146. 0,
  147. config.source_limits[self.origin_type_prefer_obj[self.combo_box.get()]],
  148. )
  149. self.entry.pack(side=tk.LEFT, padx=4, pady=8)
  150. def update_select(self, key):
  151. origin_type_prefer_list = [item.lower() for item in config.origin_type_prefer]
  152. select_value = self.origin_type_prefer_obj[
  153. self.combo_box.get()
  154. ]
  155. if self.combo_box_value < len(origin_type_prefer_list):
  156. origin_type_prefer_list[self.combo_box_value] = select_value
  157. else:
  158. origin_type_prefer_list.append(select_value)
  159. config.set(
  160. "Settings",
  161. "origin_type_prefer",
  162. ",".join(origin_type_prefer_list),
  163. )
  164. def update_input(self, event):
  165. config.set(
  166. "Settings",
  167. f"{self.origin_type_prefer_obj[self.combo_box.get()]}_num",
  168. self.entry.get(),
  169. )
  170. def change_state(self, state):
  171. self.combo_box.config(state=state)
  172. self.entry.config(state=state)