cmd/PGUI.pyw

105 lines
3.1 KiB
Python
Raw Normal View History

import tkinter
2021-02-27 21:27:43 +00:00
import tkinter.ttk
2020-01-07 22:52:22 +00:00
import os
import subprocess
import sys
2020-01-07 22:52:22 +00:00
from voussoirkit import pathclass
2020-01-07 22:52:22 +00:00
def load_programs():
directory = os.path.join(os.path.dirname(__file__), 'PGUI')
shortcuts = [os.path.join(directory, p) for p in os.listdir(directory)]
shortcuts = [p for p in shortcuts if p.lower().endswith('.lnk')]
shortcuts = [pathclass.Path(p) for p in shortcuts]
return shortcuts
2020-01-07 22:52:22 +00:00
2021-02-27 21:27:43 +00:00
class PGUILauncher(tkinter.ttk.Frame):
2020-01-07 22:52:22 +00:00
def __init__(self, parent):
2021-02-27 21:27:43 +00:00
super().__init__(parent)
2020-01-07 22:52:22 +00:00
self.parent = parent
2021-02-27 21:27:43 +00:00
self.style = tkinter.ttk.Style()
2020-01-07 22:52:22 +00:00
self.style.theme_use("clam")
2021-02-27 21:27:43 +00:00
self.pack(fill=tkinter.BOTH, expand = 1)
2020-01-07 22:52:22 +00:00
self.filter_var = tkinter.StringVar()
self.filter_var.trace('w', self.filter)
self.filter_entry = tkinter.Entry(self, textvariable=self.filter_var)
self.filter_entry.grid(row=0, column=0, columnspan=999, sticky='ew')
self.filter_entry.bind('<Return>', self.launch_filtered)
2021-07-05 05:22:10 +00:00
self.filter_entry.bind('<Escape>', self.quit)
self.filter_entry.focus()
2020-01-07 22:52:22 +00:00
x = 0
y = 1
2020-01-07 22:52:22 +00:00
self.buttons = []
2020-01-07 22:52:22 +00:00
self.buttonwidth = 12
shortcuts = load_programs()
for (index, shortcut) in enumerate(shortcuts):
2020-01-07 22:52:22 +00:00
print(y, x)
2021-02-27 21:27:43 +00:00
button = tkinter.ttk.Button(
self,
text=shortcut.replace_extension('').basename,
command=lambda sc=shortcut: self.launch_program(sc),
)
button.shortcut = shortcut
print(f'Creating button for {shortcut.basename}')
button.configure(width=self.buttonwidth)
button.grid(row=y, column=x)
self.buttons.append(button)
2020-01-07 22:52:22 +00:00
x += 1
if x >= 3 and (index != len(shortcuts)-1):
2020-01-07 22:52:22 +00:00
x = 0
y += 1
print(y, x)
self.pack()
self.update()
def filter(self, *args):
text = self.filter_entry.get().lower()
for button in self.buttons:
if text == '' or text in button['text'].lower():
button['state'] = 'normal'
else:
button['state'] = 'disabled'
def launch_filtered(self, *args):
enabled = [b for b in self.buttons if b['state'].string == 'normal']
if len(enabled) != 1:
return
button = enabled[0]
self.launch_program(button.shortcut)
def launch_program(self, shortcut):
print('opening application', shortcut.basename)
os.chdir(shortcut.parent.absolute_path)
command = f'"{shortcut.absolute_path}"'
subprocess.Popen(command, shell=True)
self.quit()
2020-01-07 22:52:22 +00:00
2021-07-05 05:22:10 +00:00
def quit(self, *args):
return super().quit()
2020-01-07 22:52:22 +00:00
def main(argv):
2021-02-27 21:27:43 +00:00
root = tkinter.Tk()
root.title("PGUI")
root.resizable(0,0)
2020-01-07 22:52:22 +00:00
ex = PGUILauncher(root)
width = root.winfo_reqwidth()
height = root.winfo_reqheight()
x_offset = (root.winfo_screenwidth() - width) / 2
y_offset = (root.winfo_screenheight() - height) / 2
root.geometry('%dx%d+%d+%d' % (width, height, x_offset, y_offset-50))
2020-01-07 22:52:22 +00:00
root.mainloop()
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))