diff --git a/requirements.txt b/requirements.txt index 37ccc94..8cabfc2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,10 @@ -customtkinter==5.2.2 -darkdetect==0.8.0 -packaging==24.1 -pyserial==3.5 +CTkMessagebox==2.7 +customtkinter==5.2.2 +darkdetect==0.8.0 +future==1.0.0 +iso8601==2.1.0 +packaging==24.1 +pillow==10.4.0 +pyserial==3.5 +PyYAML==6.0.1 +serial==0.0.97 diff --git a/src/main.py b/src/main.py index 6767f11..0a3b209 100644 --- a/src/main.py +++ b/src/main.py @@ -1,9 +1,9 @@ import customtkinter as ctk +import messagebox import serial import serial.tools import serial.tools.list_ports as ls_ports -import serial.tools.list_ports from typing import Optional, Any import functools @@ -14,9 +14,10 @@ class App(ctk.CTk): __window_height = 1350 __column_weight = [1, 9, 3] serial_status = False - serial_baudrate = 115200 - gain_values = [0.0 for i in range(5)] - gain_default = [1.3, 0.7, 1.1, 2.2, 0.5] + serial_baudrate = 9600 + gain_default = [1.30, 0.70, 1.10, 2.20, 0.50] + gain_values = gain_default.copy() + gain_values_store = gain_default.copy() gain_enable = [True for i in range(5)] def __init__(self) -> None: @@ -29,13 +30,19 @@ class App(ctk.CTk): # appearance and font ctk.set_appearance_mode("dark") ctk.set_default_color_theme("dark-blue") - ctk.set_window_scaling(0.8) - # ctk.set_widget_scaling(2.0) self.font = ctk.CTkFont(family="", size=17) # get the screen dimension screen_width = self.winfo_screenwidth() screen_height = self.winfo_screenheight() + + # high dpi support + if screen_width >= 3840 and screen_height >= 2160: + ctk.set_widget_scaling(2.0) + messagebox.isHighDIP = True + else: + ctk.set_window_scaling(0.8) + # find the center point center_x = int(screen_width / 2 - width / 2) center_y = int(screen_height / 2 - height / 2) @@ -89,7 +96,7 @@ class App(ctk.CTk): self.baud_combobox.grid(column=1, row=1, sticky=ctk.EW, pady=10, padx=20) # create Set Button self.send_button = ctk.CTkButton( - top_frame, text="设置", font=self.font, command=self.setISP + top_frame, text="应用", font=self.font, command=self.setISP ) self.send_button.grid(column=2, row=1, sticky=ctk.EW, pady=10, padx=10) return None @@ -100,22 +107,35 @@ class App(ctk.CTk): stringVar.set(f"{value:.2f}") return None - def switchSlider(i: int, slider: ctk.CTkSlider) -> None: + def switchSlider(i: int, slider: ctk.CTkSlider, value: ctk.StringVar) -> None: if slider.cget("state") == "normal": self.gain_values[i] = self.gain_default[i] + self.gain_values_store[i] = slider.get() + + value.set(f"{self.gain_default[i]:.2f}") + slider.set(self.gain_default[i]) slider.configure(button_color="grey") slider.configure(state="disabled") else: self.gain_values[i] = slider.get() + + value.set(f"{self.gain_values_store[i]:.2f}") + slider.set(self.gain_values_store[i]) slider.configure(button_color="#1f538d") slider.configure(state="normal") return None - def switchCorrection(i: int) -> None: + def switchCorrection(i: int, slider: ctk.CTkSlider) -> None: if self.gain_enable[i]: self.gain_enable[i] = False + + slider.configure(button_color="grey") + slider.configure(state="disabled") else: self.gain_enable[i] = True + + slider.configure(button_color="#1f538d") + slider.configure(state="normal") return None text = [ @@ -125,7 +145,7 @@ class App(ctk.CTk): "伽马校正系数", "饱和度调整系数", ] - gain_min = [-2.0, -2.0, -2.0, 0.0, -1.0] + gain_min = [0.0, 0.0, 0.0, 0.0, -1.0] gain_max = [2.0, 2.0, 2.0, 5.0, 1.0] fg_clolor = [ "#4d140b", @@ -162,7 +182,6 @@ class App(ctk.CTk): gain_frame[i], text=text[i], font=self.font, - command=functools.partial(switchCorrection, i), ) ) gain_checkbox[i - 3].select() @@ -177,9 +196,15 @@ class App(ctk.CTk): gain_str.append( ctk.StringVar(gain_slider[i], f"{self.gain_default[i]:.2f}") ) + + # set command gain_slider[i].configure( command=functools.partial(setGain, i=i, stringVar=gain_str[i]) ) + if i >= 3: + gain_checkbox[i - 3].configure( + command=functools.partial(switchCorrection, i, gain_slider[i]) + ) # gain value ctk.CTkLabel(gain_frame[i], text=str(gain_min[i]), font=self.font).pack( @@ -202,7 +227,9 @@ class App(ctk.CTk): ctk.CTkButton( data_frame, text="使用推荐配置", - command=functools.partial(switchSlider, i=i, slider=gain_slider[i]), + command=functools.partial( + switchSlider, i=i, slider=gain_slider[i], value=gain_str[i] + ), font=self.font, ) ) @@ -232,13 +259,17 @@ class App(ctk.CTk): print(self.select_port) except AttributeError: print("Please Select Serial Port") + messagebox.showError("Please Select Serial Port") else: if not self.serial_status: try: self.serial = serial.Serial(self.select_port, self.serial_baudrate) - self.serial.open() except AttributeError: - print("Failed to Connect Serial") + print("Please Select Serial Port") + messagebox.showError("Please Select Serial Port") + except serial.serialutil.SerialException: + print("Can't Control the Serial Port") + messagebox.showError("Can't Control the Serial Port") else: self.serial_status = True self.serial_button_text.set("断开连接") @@ -249,6 +280,7 @@ class App(ctk.CTk): self.serial.close() except Exception: print("Failed to Close Serial") + messagebox.showError("Failed to Close Serial") else: self.serial_status = False self.serial_button_text.set("连接") @@ -263,6 +295,7 @@ class App(ctk.CTk): self.serial.open() except Exception: print("Failed to Set Serial Baudrate") + messagebox.showError("Failed to Set Serial Baudrate") else: print("Succeed to Set Serial Baudrate") else: @@ -272,22 +305,27 @@ class App(ctk.CTk): def switchSign(i: int) -> str: match i: case 0: - return 'r' + return "cmdr" case 1: - return 'g' + return "cmdg" case 2: - return 'b' + return "cmdb" case 3: - return 'a' + return "cmda" case 4: - return 's' + return "cmds" case _: - return 'error' + return "error" + + def toAscii(num: float) -> str: + return chr(int(num)) if self.serial_status: for i in range(5): if self.gain_enable[i]: - self.serial.write(f"{switchSign(i)}{bin(int(self.gain_values[i]))}{bin(int(self.gain_values[i] % 1 * 256))}") + bytes = f"{switchSign(i)}{toAscii(self.gain_values[i])}{toAscii(self.gain_values[i] % 1 * 256)}" + print(f"Send:{bytes}") + self.serial.write(bytes.encode()) else: print("Please Connect to Serial") diff --git a/src/messagebox.py b/src/messagebox.py new file mode 100644 index 0000000..9bdac7d --- /dev/null +++ b/src/messagebox.py @@ -0,0 +1,83 @@ +from typing import Literal +from CTkMessagebox import CTkMessagebox + +isHighDIP = False + +class Message(CTkMessagebox): + def __init__( + self, + master: any = None, + width: int = 400, + height: int = 200, + title: str = "CTkMessagebox", + message: str = "This is a CTkMessagebox!", + option_1: str = "OK", + option_2: str = None, + option_3: str = None, + options: list = ..., + border_width: int = 1, + border_color: str = "default", + button_color: str = "default", + bg_color: str = "default", + fg_color: str = "default", + text_color: str = "default", + title_color: str = "default", + button_text_color: str = "default", + button_width: int = None, + button_height: int = None, + cancel_button_color: str = None, + cancel_button: str = None, + button_hover_color: str = "default", + icon: str = "info", + icon_size: tuple = None, + corner_radius: int = 15, + justify: str = "right", + font: tuple = None, + header: bool = False, + topmost: bool = True, + fade_in_duration: int = 0, + sound: bool = False, + wraplength: int = 0, + option_focus: Literal[1] | Literal[2] | Literal[3] = None, + isHighDPI: bool = False, + ): + super().__init__( + master, + width, + height, + title, + message, + option_1, + option_2, + option_3, + options, + border_width, + border_color, + button_color, + bg_color, + fg_color, + text_color, + title_color, + button_text_color, + button_width, + button_height, + cancel_button_color, + cancel_button, + button_hover_color, + icon, + icon_size, + corner_radius, + justify, + font, + header, + topmost, + fade_in_duration, + sound, + wraplength, + option_focus, + ) + if isHighDPI: + self._set_scaling(1.7, 1.7) + +def showError(message:str)->None: + Message(title="Error", message=message, icon="cancel", isHighDPI=isHighDIP) \ No newline at end of file