Add filter box to PGUI for keyboard-driven launching.

This commit is contained in:
voussoir 2021-02-27 13:24:46 -08:00
parent 26c95d14d3
commit 337e46ec76
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -1,3 +1,4 @@
import tkinter
from tkinter import Button as tButton from tkinter import Button as tButton
from tkinter import Tk, BOTH from tkinter import Tk, BOTH
from tkinter.ttk import Frame, Style, Button from tkinter.ttk import Frame, Style, Button
@ -24,21 +25,31 @@ class PGUILauncher(Frame):
self.style.theme_use("clam") self.style.theme_use("clam")
self.pack(fill=BOTH, expand = 1) self.pack(fill=BOTH, expand = 1)
x = 0 self.filter_var = tkinter.StringVar()
y = 0 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)
self.filter_entry.focus()
x = 0
y = 1
self.buttons = []
self.buttonwidth = 12 self.buttonwidth = 12
shortcuts = load_programs() shortcuts = load_programs()
for (index, shortcut) in enumerate(shortcuts): for (index, shortcut) in enumerate(shortcuts):
print(y, x) print(y, x)
newButton = Button( button = Button(
self, self,
text=shortcut.replace_extension('').basename, text=shortcut.replace_extension('').basename,
command=lambda sc=shortcut: self.launch_program(sc), command=lambda sc=shortcut: self.launch_program(sc),
) )
button.shortcut = shortcut
print(f'Creating button for {shortcut.basename}') print(f'Creating button for {shortcut.basename}')
newButton.configure(width=self.buttonwidth) button.configure(width=self.buttonwidth)
newButton.grid(row=y, column=x) button.grid(row=y, column=x)
self.buttons.append(button)
x += 1 x += 1
if x >= 3 and (index != len(shortcuts)-1): if x >= 3 and (index != len(shortcuts)-1):
x = 0 x = 0
@ -48,6 +59,22 @@ class PGUILauncher(Frame):
self.pack() self.pack()
self.update() self.update()
def filter(self, *args):
text = self.filter_entry.get().lower()
for button in self.buttons:
if text == '':
button['state'] = 'normal'
elif text not in button['text'].lower():
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): def launch_program(self, shortcut):
print('opening application', shortcut.basename) print('opening application', shortcut.basename)
os.chdir(shortcut.parent.absolute_path) os.chdir(shortcut.parent.absolute_path)