Betree / magicblue

💡 Unofficial Python API to control Magic Blue bulbs over Bluetooth
MIT License
101 stars 23 forks source link

Tkinter GUI #41

Open kaitoxakimoto opened 7 years ago

kaitoxakimoto commented 7 years ago

it's a very very simple prototype (in fact, i declare the use of my bulb's mac in the first declaration) but i think the use of Tkinter could be very useful to implement!

from tkinter import *
from magicblue import MagicBlue

#CHANGE THE BULB MAC HERE
bulb_mac_address = 'F8:1D:78:60:8E:0F'
bulb = MagicBlue(bulb_mac_address, 10) # Replace 9 by whatever your version is (default: 7)
bulb.connect()

def switch():
    global first
    if first:
        return
    try:
        inf=bulb.get_device_info()
    except:
        bulb.connect()
        inf=bulb.get_device_info()
    if inf["on"]:
        bulb.turn_off()
    else:
        bulb.turn_on()

def change_red(re):
    global first
    if first:
        return
    try:
        inf=bulb.get_device_info()
    except:
        bulb.connect()
    color=map(int,(r.get(),g.get(),b.get()))
    bulb.set_color(color)

def change_green(gr):
    global first
    if first:
        return
    try:
        inf=bulb.get_device_info()
    except:
        bulb.connect()
    color=map(int,(r.get(),g.get(),b.get()))
    bulb.set_color(color)

def change_blue(bl):
    global first
    if first:
        return
    try:
        inf=bulb.get_device_info()
    except:
        bulb.connect()
    color=map(int,(r.get(),g.get(),b.get()))
    bulb.set_color(color)

def change_bright(br):
    global first
    if first:
        first = False
        return
    try:
        inf=bulb.get_device_info()
    except:
        bulb.connect()
    bulb.set_warm_light(bright.get()/255.0) 

w = Tk()    

#Models

r=IntVar()
g=IntVar()
b=IntVar()
bright=IntVar()

#First Check
inf=bulb.get_device_info()
r.set(inf["r"])
g.set(inf["g"])
b.set(inf["b"])
bright.set(inf["brightness"])
first=True

#Power Button

switch=Button(w,text="On / Off",command=switch)
switch.pack()

#First Frame

color_frame=Frame(w)
color_frame.pack()

#RGB

rgb_frame=Frame(color_frame,bd=10)
rgb_frame.grid(row=0,column=0)

red=Scale(rgb_frame, variable = r,orient=HORIZONTAL,to = 255, command = change_red, resolution = 5)
Label(rgb_frame,text="Red").pack()
red.pack()

green=Scale(rgb_frame, variable = g,orient=HORIZONTAL,to = 255, command = change_green, resolution = 5)
Label(rgb_frame,text="Green").pack()
green.pack()

blue=Scale(rgb_frame, variable = b , orient = HORIZONTAL , to = 255, command = change_blue, resolution = 5)
Label(rgb_frame,text="Blue").pack()
blue.pack()

#Bright

bright_frame=Frame(color_frame,bd=10)
bright_frame.grid(row=0,column=1)

bright=Scale(bright_frame, variable = bright , orient = VERTICAL ,from_ = 255, to = 0   , command = change_bright, resolution = -5)
Label(bright_frame,text="Brightness").pack()
bright.pack()

w.mainloop()

bulb.disconnect()

it looks like this for now: image

Betree commented 7 years ago

This is cool! We have to think about how relevant it is to integrate it in this project as this is mainly used on Raspberry Pi or shell scripts at the moment, but I like the idea of a simple GUI that could be useful for "non-technical" users willing to use their bulbs from their PC.

Any arguments or ideas are welcome

styfle commented 7 years ago

My python skills are not great, but I would imagine you can distribute a separate magicblue-gui package that depends on the magicblue package via pip, correct?

...After looking at the docker-compose package, it looks like these dependencies are called "Requires Distributions"

Betree commented 7 years ago

@styfle Yes, I prefer this approach rather than integrating a GUI directly on this project