-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
132 lines (105 loc) · 4.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import database
import tkinter as tk
import tkinter.ttk as tkk
from tkinter import messagebox
from yeelight import Bulb
class mainWindow(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
master.title('Simple Yeelight On/Off - CRUD')
master.geometry("+{}+{}".format(200, 200))
# instance database
self.db = database.createDB()
self.pack()
# Create Window
self.createWid()
def createWid(self):
# Create frames
frame1 = tk.Frame(self)
frame1.pack(side=tk.TOP, fill=tk.BOTH, padx=160, pady=10, anchor='center')
frame2 = tk.Frame(self)
frame2.pack(fill=tk.BOTH, expand=True, padx=15)
frame3 = tk.Frame(self)
frame3.pack(side=tk.BOTTOM, padx=5)
# Labels
name_label = tk.Label(frame1, text='Name *')
name_label.grid(row=0, column=0)
ip_label = tk.Label(frame1, text='IP *')
ip_label.grid(row=0, column=1)
location_label = tk.Label(frame1, text='Location')
location_label.grid(row=0, column=2)
# Entry Text
self.req_name = tk.Entry(frame1)
self.req_name.grid(row=1, column=0)
self.req_ip = tk.Entry(frame1)
self.req_ip.grid(row=1, column=1, padx=10)
self.req_location = tk.Entry(frame1)
self.req_location.grid(row=1, column=2)
# Add Button
button_add = tk.Button(frame1, text='Add Yeelight', bg='#FF6700', fg='black')
button_add['font'] = ("Arial", "12", "bold")
button_add['command'] = self.addBulb
button_add.grid(row=0, column=4, rowspan=2, padx=10)
# Treeview.
self.treeview = tkk.Treeview(frame2, columns=('Nome', 'IP', 'Localização'))
self.treeview.heading('#0', text='ID')
self.treeview.heading('#1', text='Name')
self.treeview.heading('#2', text='IP')
self.treeview.heading('#3', text='Location')
for row in self.db.getBulb():
self.treeview.insert('', 'end', text=row[0], values=(row[1], row[2], row[3]))
self.treeview.pack(fill=tk.BOTH, expand=True)
# Delete Button
button_delete = tk.Button(frame3, text='Delete', bg='#E71818', fg='white')
button_delete['font'] = ("Arial", "12", "bold")
button_delete['command'] = self.deleteBulb
button_delete.pack(side='left', padx=10, pady=10)
# Turn On Button
button_on = tk.Button(frame3, text="Turn On", bg="black", fg='white')
button_on['font'] = ("Arial", "12", "bold")
button_on['command'] = self.turnOn
button_on.pack(side='left', padx=10, pady=10)
# Turn Off Button
button_off = tk.Button(frame3, text="Turn Off", bg="black", fg='white')
button_off['font'] = ("Arial", "12", "bold")
button_off['command'] = self.turnOff
button_off.pack(side='left', padx=10, pady=10)
def turnOn(self):
if not self.treeview.focus():
messagebox.showwarning('Ops..', 'No item selected')
else:
select_bulb = self.treeview.focus()
res = self.treeview.item(select_bulb)
ip = res['values'][1]
bulb = Bulb(ip)
bulb.turn_on()
def turnOff(self):
if not self.treeview.focus():
messagebox.showwarning('Ops..', 'No item selected')
else:
select_bulb = self.treeview.focus()
res = self.treeview.item(select_bulb)
ip = res['values'][1]
bulb = Bulb(ip)
bulb.turn_off()
def addBulb(self):
name = self.req_name.get()
ip = self.req_ip.get()
location = self.req_location.get()
self.db.addBulb(name=name, ip=ip, location=location)
id = self.db.getLastBulb()[0]
self.treeview.insert('', 'end', text=id, values=(name, ip, location))
def deleteBulb(self):
if not self.treeview.focus():
messagebox.showwarning('Ops..', 'No item selected')
else:
selected = self.treeview.focus()
id = self.treeview.item(selected)
self.db.deleteBulb(id['text'])
self.treeview.delete(selected)
root = tk.Tk()
root.iconbitmap('icon.ico')
app = mainWindow(master=root)
app.mainloop()