0<0# : ^
'''
@echo off
set script=%~f0
python -x "%script%" %*
exit /b 0
'''
import subprocess
import sys
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
# Install packages
try:
import tkinter
except ImportError:
install('tkinter')
try:
from bs4 import BeautifulSoup
except ImportError:
install('beautifulsoup4')
try:
from tkinter import ttk
except ImportError:
install('ttk')
# Now you can import your packages and run your GUI
import tkinter as tk
from bs4 import BeautifulSoup
from tkinter import ttk
class ManualSearch:
def __init__(self, root):
self.root = root
self.entry_text = tk.StringVar()
self.entry = tk.Entry(root, textvariable=self.entry_text, font=('TkDefaultFont', 12, 'bold'), fg='red')
self.entry.pack()
self.result = tk.Text(root, font=('TkDefaultFont', 12, 'bold')) # Set the default font to bold
self.result.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # Pack the result Text widget to the left
self.entry.bind('<KeyRelease>', self.search)
# Create a frame for the listbox and scrollbar
self.frame = tk.Frame(root)
self.frame.pack(side=tk.RIGHT, fill=tk.Y) # Pack the frame to the right
# Create a scrollbar
self.scrollbar = tk.Scrollbar(self.frame)
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Create a listbox with bold font and blue text
self.listbox = tk.Listbox(self.frame, yscrollcommand=self.scrollbar.set, font=('TkDefaultFont', 12, 'bold'), fg='red')
self.listbox.pack(side=tk.LEFT, fill=tk.BOTH)
self.scrollbar.config(command=self.listbox.yview)
# Bind the listbox click event to a function
self.listbox.bind('<<ListboxSelect>>', self.on_select)
self.load_file()
def load_file(self):
try:
with open('OpenBORManual - DCEmulation.html', 'r', encoding='utf-8') as f:
contents = f.read()
self.soup = BeautifulSoup(contents, 'html.parser')
# Parse the terms from the HTML and add them to the listbox
terms = [term.text for term in self.soup.find_all('b') if term.text.strip()]
terms.sort() # Sort the terms in alphabetical order
for term in terms:
self.listbox.insert(tk.END, term)
except Exception as e:
print(f"Error loading file: {e}")
def search(self, event):
query = self.entry_text.get().lower() # Convert query to lowercase
self.result.delete('1.0', tk.END)
self.result.tag_config('bold', font=('TkDefaultFont', 12, 'bold'))
self.result.tag_config('red', foreground='red') # Create a new tag for red text
self.result.tag_config('bold_red', foreground='red', font=('TkDefaultFont', 12, 'bold')) # Create a new tag for bold and red text
found = False
for term in self.soup.find_all('b'):
if term.text.lower().startswith(query): # Convert term to lowercase
found = True
self.display_term(term)
if not found:
for term in self.soup.find_all('b'):
if query in term.text.lower(): # Convert term to lowercase
self.display_term(term)
def display_term(self, term):
# Create new tags for blue and red text
self.result.tag_config('blue', foreground='blue')
self.result.tag_config('red', foreground='red')
# Find the nearest preceding h1 or h2 tag
h2_category = term.find_previous('h2')
h1_category = term.find_previous('h1')
# Check if h1 or h2 tag found and insert it into the result Text widget
if h2_category and (not h1_category or h2_category.find_previous('h1') == h1_category):
self.result.insert(tk.END, f'{h2_category.text}\n', 'blue') # Apply the 'blue' tag
if h1_category:
self.result.insert(tk.END, f'{h1_category.text}\n', 'blue') # Apply the 'blue' tag
self.result.insert(tk.END, f'{term.text}\n', 'red') # Apply the 'red' tag
# Find the next 'ul' tag
explanation = term.find_next('ul')
if explanation:
# Iterate over each 'li' tag within the 'ul' tag
for li in explanation.find_all('li'):
self.result.insert(tk.END, f'{li.text}\n\n')
# Find the next 'p' tag after the 'ul' tag
next_p = explanation.find_next_sibling('p')
if next_p:
self.result.insert(tk.END, f'{next_p.text}\n\n')
def on_select(self, event):
# Get the selected term
selected_term = self.listbox.get(self.listbox.curselection())
# Set the entry text to the selected term
self.entry_text.set(selected_term)
# Trigger the search function
self.search(None)
root = tk.Tk()
root.title("OpenBOR Manual reference")
# Scale the GUI by a factor of 1
root.tk.call('tk', 'scaling', 1.0)
app = ManualSearch(root)
root.mainloop()