Untitled
unknown
plain_text
a year ago
8.5 kB
4
Indexable
class TouchKeyboard(ctk.CTkFrame):
def __init__(self, parent, entry_widget, dark_mode=False):
super().__init__(
parent,
fg_color='#2c2c2e' if dark_mode else '#e5e5e7'
)
self.entry_widget = entry_widget
self.dark_mode = dark_mode
# Define keyboard layouts
self.layouts = {
'normal': [
['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'],
['⇧', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '⌫'],
['123', ' ', '.', 'Connect']
],
'shift': [
['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'],
['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'],
['⇧', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '⌫'],
['123', ' ', '.', 'Connect']
],
'symbols': [
['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
['@', '#', '$', '_', '&', '-', '+', '(', ')', '/'],
['=', '*', '"', '\'', ':', ';', '!', '?', '\\'],
['ABC', '<', '>', '{', '}', '[', ']', '⌫'],
['ABC', ' ', '.', 'Connect']
]
}
self.current_layout = 'normal'
self.create_keyboard()
def create_keyboard(self):
# Clear existing keyboard
for widget in self.winfo_children():
widget.destroy()
# Set button colors based on dark mode
button_color = '#3a3a3c' if self.dark_mode else '#ffffff'
hover_color = '#2c2c2e' if self.dark_mode else '#e5e5e7'
text_color = 'white' if self.dark_mode else 'black'
# Create buttons for current layout
layout = self.layouts[self.current_layout]
for i, row in enumerate(layout):
row_frame = ctk.CTkFrame(self, fg_color='transparent')
row_frame.pack(expand=True, fill='both', padx=2, pady=2)
for j, key in enumerate(row):
if key == 'Connect':
# Special styling for Connect button
button = ctk.CTkButton(
row_frame,
text=key,
command=lambda k=key: self.handle_key(k),
fg_color='#34c759',
hover_color='#30d158',
text_color='white',
height=60
)
else:
# Regular key styling
button = ctk.CTkButton(
row_frame,
text=key,
command=lambda k=key: self.handle_key(k),
fg_color=button_color,
hover_color=hover_color,
text_color=text_color,
height=60
)
# Adjust button width based on key
if key in [' ']:
button.pack(side='left', expand=True, fill='x', padx=2, pady=2)
elif key in ['Connect']:
button.pack(side='left', expand=True, fill='x', padx=2, pady=2)
else:
button.pack(side='left', expand=True, padx=2, pady=2)
def handle_key(self, key):
if key == '⌫':
# Handle backspace
current_text = self.entry_widget.get()
self.entry_widget.delete(0, 'end')
self.entry_widget.insert(0, current_text[:-1])
elif key == '⇧':
# Toggle between normal and shift layouts
self.current_layout = 'shift' if self.current_layout == 'normal' else 'normal'
self.create_keyboard()
elif key in ['123', 'ABC']:
# Toggle between letters and symbols
self.current_layout = 'symbols' if key == '123' else 'normal'
self.create_keyboard()
elif key == 'Connect':
# Trigger connect action in parent
self.event_generate('<<ConnectPressed>>')
else:
# Insert regular key
current_pos = self.entry_widget.index('insert')
self.entry_widget.insert(current_pos, key)
class NetworkManager:
def __init__(self):
self.interface = self._get_wireless_interface()
def _get_wireless_interface(self):
try:
output = subprocess.check_output(['iwconfig'], stderr=subprocess.STDOUT).decode()
interfaces = re.findall(r'(\w+)\s+IEEE', output)
return interfaces[0] if interfaces else 'wlan0'
except:
return 'wlan0'
def scan_networks(self):
try:
# Kill any existing wpa_supplicant processes
subprocess.run(['sudo', 'killall', 'wpa_supplicant'], stderr=subprocess.DEVNULL)
# Scan for networks
output = subprocess.check_output(['sudo', 'iwlist', self.interface, 'scan']).decode()
networks = []
current_network = {}
for line in output.split('\n'):
line = line.strip()
if 'Cell' in line:
if current_network:
networks.append(current_network)
current_network = {}
if 'ESSID' in line:
ssid = re.findall(r'ESSID:"([^"]*)"', line)
if ssid:
current_network['ssid'] = ssid[0]
if 'Quality' in line:
quality = re.findall(r'Quality=(\d+)/70', line)
if quality:
# Convert to percentage
current_network['signal'] = int(int(quality[0]) * 100 / 70)
if current_network:
networks.append(current_network)
return sorted(networks, key=lambda x: x.get('signal', 0), reverse=True)
except Exception as e:
print(f"Error scanning networks: {e}")
return []
def get_current_connection(self):
try:
output = subprocess.check_output(['iwconfig', self.interface]).decode()
essid = re.findall(r'ESSID:"([^"]*)"', output)
if essid and essid[0]:
return essid[0]
except:
pass
return None
def get_ip_address(self):
try:
output = subprocess.check_output(['ip', 'addr', 'show', self.interface]).decode()
ip = re.findall(r'inet\s+(\d+\.\d+\.\d+\.\d+)', output)
return ip[0] if ip else None
except:
return None
def connect_to_network(self, ssid, password):
try:
# Create wpa_supplicant configuration
config = f"""
network={{
ssid="{ssid}"
psk="{password}"
key_mgmt=WPA-PSK
}}
"""
# Write configuration to temporary file
with open('/tmp/wpa_supplicant.conf', 'w') as f:
f.write(config)
# Stop any existing wpa_supplicant processes
subprocess.run(['sudo', 'killall', 'wpa_supplicant'], stderr=subprocess.DEVNULL)
# Connect using wpa_supplicant
subprocess.Popen([
'sudo', 'wpa_supplicant',
'-B', # Run in background
'-i', self.interface,
'-c', '/tmp/wpa_supplicant.conf'
])
# Wait for connection
time.sleep(2)
# Get IP using DHCP
subprocess.run(['sudo', 'dhclient', self.interface])
return True
except Exception as e:
print(f"Error connecting to network: {e}")
return FalseEditor is loading...
Leave a Comment