Add multiple servers

This commit is contained in:
Jimmy 2024-08-30 17:35:26 +12:00
parent d1b9de736f
commit 161fd5e12f
1 changed files with 64 additions and 31 deletions

95
main.py
View File

@ -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("<ButtonRelease-1>", 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("<ButtonRelease-1>", 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("<ButtonRelease-1>", 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("<ButtonRelease-1>", 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("<ButtonRelease-1>", 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("<ButtonRelease-1>", 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("<ButtonRelease-1>", 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("<ButtonRelease-1>", 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()
app = App()