-
Notifications
You must be signed in to change notification settings - Fork 0
/
btify.py
executable file
·91 lines (73 loc) · 2.79 KB
/
btify.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
#!/usr/bin/env python3
import gi
import subprocess
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
# Define your CSS styles here
css_data = """
/* Define CSS classes for different states */
.running {
background-color: #00FF00; /* Green */
}
.stopped {
background-color: #FF0000; /* Red */
}
.error {
background-color: #FF0000; /* Red */
}
"""
# Function to start the Bluetooth service
def start_bluetooth(widget):
try:
subprocess.run(["sudo", "systemctl", "start", "bluetooth.service"], check=True)
status_label.set_text("Running")
status_label.get_style_context().add_class("running") # Apply CSS class for running
except subprocess.CalledProcessError:
status_label.set_text(f"Error starting: {subprocess.CalledProcessError.output}")
# Function to stop the Bluetooth service
def stop_bluetooth(widget):
try:
subprocess.run(["sudo", "systemctl", "stop", "bluetooth.service"], check=True)
status_label.set_text("Stopped")
status_label.get_style_context().add_class("stopped") # Apply CSS class for stopped
except subprocess.CalledProcessError:
status_label.set_text("Error stopping")
def get_initial_bluetooth_status():
try:
result = subprocess.run(["sudo", "systemctl", "is-active", "bluetooth.service"], stdout=subprocess.PIPE)
return result.stdout.decode().strip()
except subprocess.CalledProcessError:
return "Error"
# Create the GTK window
window = Gtk.Window(title="Bluetooth Control")
window.connect("destroy", Gtk.main_quit)
window.set_default_size(400, 300) # Set the window size
window.set_resizable(False) # Disable window resizing
# Create buttons
start_button = Gtk.Button(label="Start Bluetooth")
start_button.connect("clicked", start_bluetooth)
stop_button = Gtk.Button(label="Stop Bluetooth")
stop_button.connect("clicked", stop_bluetooth)
# Create a label for status and set the initial status
initial_status = get_initial_bluetooth_status()
status_label = Gtk.Label()
status_label.set_text(f"Status: {initial_status}")
status_label.set_margin_start(10)
status_label.set_margin_end(10)
# Create a vertical box to hold the widgets
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
vbox.pack_start(start_button, False, False, 0)
vbox.pack_start(stop_button, False, False, 0)
vbox.pack_start(status_label, False, False, 0)
# Add the box to the window
window.add(vbox)
# Load CSS styles from the embedded data
css_provider = Gtk.CssProvider()
css_provider.load_from_data(css_data.encode()) # Load the CSS data
screen = Gdk.Screen.get_default()
style_context = Gtk.StyleContext()
style_context.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
# Show all widgets
window.show_all()
# Start the GTK main loop
Gtk.main()