From 161fd5e12f3f197e7f581068cf4ad4b64d0d7533 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Fri, 30 Aug 2024 17:35:26 +1200 Subject: [PATCH] Add multiple servers --- main.py | 95 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 31 deletions(-) diff --git a/main.py b/main.py index 79e37a5..276c6b8 100644 --- a/main.py +++ b/main.py @@ -11,44 +11,77 @@ class App: self.root = tk.Tk() self.root.attributes('-fullscreen', True) - # Red Slider - self.red_slider = tk.Scale(self.root, from_=255, to=0, orient=tk.VERTICAL, length=500, width=50, label="Red") - self.red_slider.set(0) - self.red_slider.pack(side=tk.LEFT, padx=10) - self.red_slider.bind("", self.update_colour) + # List of servers (IP, Port) + self.servers = [("192.168.0.206", 80), ("192.168.0.208", 80)] - self.green_slider = tk.Scale(self.root, from_=255, to=0, orient=tk.VERTICAL, length=500, width=50, label="Green") - self.green_slider.set(0) - self.green_slider.pack(side=tk.LEFT, padx=10) - self.green_slider.bind("", self.update_colour) + # Create Notebook for tabs + self.notebook = ttk.Notebook(self.root) + self.notebook.pack(expand=1, fill="both") - - self.blue_slider = tk.Scale(self.root, from_=255, to=0, orient=tk.VERTICAL, length=500, width=50, label="Blue") - self.blue_slider.set(0) - self.blue_slider.pack(side=tk.LEFT, padx=10) - self.blue_slider.bind("", self.update_colour) - - - self.brightness_slider = tk.Scale(self.root, from_=255, to=0, orient=tk.VERTICAL, length=500, width=50, label="Brighness") - self.brightness_slider.set(127) - self.brightness_slider.pack(side=tk.LEFT, padx=10) - self.brightness_slider.bind("", self.update_colour) + # Create a tab for each server + self.tabs = {} + for server in self.servers: + tab = ttk.Frame(self.notebook) + self.notebook.add(tab, text=f"{server[0]}:{server[1]}") + self.create_sliders(tab) + self.tabs[server] = tab async_mainloop(self.root) + def create_sliders(self, tab): + """Create sliders for each tab.""" + slider_length = 800 + slider_width = 50 + + # Red Slider + red_slider = tk.Scale(tab, from_=255, to=0, orient=tk.VERTICAL, length=slider_length, width=slider_width, label="Red") + red_slider.set(0) + red_slider.pack(side=tk.LEFT, padx=10) + red_slider.bind("", lambda event, srv=tab: self.update_colour(event, srv)) + + # Green Slider + green_slider = tk.Scale(tab, from_=255, to=0, orient=tk.VERTICAL, length=slider_length, width=slider_width, label="Green") + green_slider.set(0) + green_slider.pack(side=tk.LEFT, padx=10) + green_slider.bind("", lambda event, srv=tab: self.update_colour(event, srv)) + + # Blue Slider + blue_slider = tk.Scale(tab, from_=255, to=0, orient=tk.VERTICAL, length=slider_length, width=slider_width, label="Blue") + blue_slider.set(0) + blue_slider.pack(side=tk.LEFT, padx=10) + blue_slider.bind("", lambda event, srv=tab: self.update_colour(event, srv)) + + # Brightness Slider + brightness_slider = tk.Scale(tab, from_=255, to=0, orient=tk.VERTICAL, length=slider_length, width=slider_width, label="Brightness") + brightness_slider.set(127) + brightness_slider.pack(side=tk.LEFT, padx=10) + brightness_slider.bind("", lambda event, srv=tab: self.update_colour(event, srv)) + + tab.widgets = { + 'red_slider': red_slider, + 'green_slider': green_slider, + 'blue_slider': blue_slider, + 'brightness_slider': brightness_slider, + } + @async_handler - async def update_colour(self, event): - print(event) - r = self.red_slider.get() - g = self.green_slider.get() - b = self.blue_slider.get() - brightness = self.brightness_slider.get() - color = color_utils.adjust_brightness((r,b,g), brightness) + async def update_colour(self, event, tab): + """Update color based on the slider values and send to the selected server.""" + red_slider = tab.widgets['red_slider'] + green_slider = tab.widgets['green_slider'] + blue_slider = tab.widgets['blue_slider'] + brightness_slider = tab.widgets['brightness_slider'] + + r = red_slider.get() + g = green_slider.get() + b = blue_slider.get() + brightness = brightness_slider.get() + + color = color_utils.adjust_brightness((r, b, g), brightness) print(color, brightness) - await send_to_server(bytes(color), ("192.168.0.206","80")) - - + selected_server = self.notebook.tab(self.notebook.select(), "text").split(":") + await send_to_server(bytes(color), (selected_server[0], int(selected_server[1]))) if __name__ == "__main__": - app = App() \ No newline at end of file + app = App()