Add tkinter interface, it works

master
Sergio Alvariño 3 years ago
parent df7a6c888e
commit 79387aa0d7

@ -48,4 +48,5 @@ if not os.path.exists(menu_file):
os.strerror(errno.ENOENT), menu_file) os.strerror(errno.ENOENT), menu_file)
Menu(get_data_from(menu_file)).get_command().execute() # Menu(get_data_from(menu_file)).get_command().execute()
Menu(get_data_from(menu_file)).get_option()

@ -8,6 +8,9 @@ from collections import OrderedDict
from getkey import getkey, keys from getkey import getkey, keys
import subprocess import subprocess
import tkinter as tk
def get_data_from(menu_file): def get_data_from(menu_file):
"""Get menu data from a yaml menu file.""" """Get menu data from a yaml menu file."""
@ -24,6 +27,8 @@ def get_data_from(menu_file):
return(menu_data) return(menu_data)
class Command: class Command:
"""A Mach5 command.""" """A Mach5 command."""
@ -42,6 +47,7 @@ class Command:
def execute(self): def execute(self):
"""Execute command.""" """Execute command."""
print(f"Execute command {self._name}")
subprocess.Popen(os.path.expanduser(self._cmd)) subprocess.Popen(os.path.expanduser(self._cmd))
@ -73,6 +79,13 @@ class Menu:
my_str += f"{v.show()}\n" my_str += f"{v.show()}\n"
return my_str return my_str
def show_entries(self):
"""Show Menu entries."""
my_str = ''
for i, v in self._entries.items():
my_str += f"{v.show()}\n"
return my_str
def get_command(self): def get_command(self):
"""Get command from menu.""" """Get command from menu."""
print(self.show(True)) print(self.show(True))
@ -85,3 +98,29 @@ class Menu:
return self._entries[opt] return self._entries[opt]
elif opt == keys.ESC: elif opt == keys.ESC:
exit(0) exit(0)
def get_option(self):
"""Get option with Tkinter."""
def key_pressed(event):
"""Return char for key_pressed event."""
opt = event.char
if opt in self._entries:
if isinstance(self._entries[opt], Menu):
return self._entries[opt].get_option()
else:
root.destroy()
self._entries[opt].execute()
exit(0)
elif opt == keys.ESC:
exit(0)
root = tk.Tk()
root.geometry('300x100')
root.title("Mach5")
text_box = tk.Text(root)
text_box.insert(tk.END, self.show(True))
text_box.grid(row=0, column=0)
root.bind("<Any-KeyPress>", key_pressed)
root.mainloop()

Loading…
Cancel
Save