0% found this document useful (0 votes)
12 views3 pages

message (3)

This document contains a Python script that creates a GUI application using Tkinter for searching text files in a specified directory based on user-defined criteria (name, domain, and IP address). The application allows users to select a directory, input search terms, and displays the results in a text area. It also includes error handling for file reading and dynamic resizing of the GUI components based on the window size.

Uploaded by

tokisu583
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

message (3)

This document contains a Python script that creates a GUI application using Tkinter for searching text files in a specified directory based on user-defined criteria (name, domain, and IP address). The application allows users to select a directory, input search terms, and displays the results in a text area. It also includes error handling for file reading and dynamic resizing of the GUI components based on the window size.

Uploaded by

tokisu583
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import os

import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk

def verzeichnis_waehlen():
verzeichnis = filedialog.askdirectory()
if verzeichnis:
verzeichnis_var.set(verzeichnis)

def durchsuchen():
ausgabe_text.delete("1.0", tk.END)
wurzelverzeichnis = verzeichnis_var.get()
suchname = name_var.get().lower()
suchdomain = domain_var.get().lower()
suchip = ip_var.get().lower()

for ordnername, unterordner, dateien in os.walk(wurzelverzeichnis):


for datei in dateien:
if datei.endswith(".txt"):
dateipfad = os.path.join(ordnername, datei)
try:
with open(dateipfad, "r", encoding="utf-8", errors="ignore") as
f:
inhalt = f.read().lower()
if (suchname in inhalt) and (suchdomain in inhalt) and
(suchip in inhalt):
ausgabe_text.insert(tk.END, f"Treffer in: {dateipfad}\
n", "highlight")
except Exception as e:
ausgabe_text.insert(tk.END, f"Fehler beim Lesen von
{dateipfad}: {e}\n")

# Fenster erstellen
root = tk.Tk()
root.title("DB Searcher")
root.geometry("1200x800")

# Hintergrundbild laden
try:
bg_image = Image.open("eagle.png")
bg_photo = ImageTk.PhotoImage(bg_image)
except Exception as e:
print(f"Fehler beim Laden des Hintergrundbildes: {e}")
bg_photo = None

# Canvas für Hintergrund


canvas = tk.Canvas(root, bg="black", highlightthickness=0)
canvas.pack(fill="both", expand=True)

# Hintergrundbild einfügen (falls vorhanden)


if bg_photo:
bg_id = canvas.create_image(0, 0, image=bg_photo, anchor="nw")

# Hauptcontainer für alle Elemente


main_frame = tk.Frame(canvas, bg='black', bd=0)
canvas.create_window((0, 0), window=main_frame, anchor="nw", tags="main_frame")

# Eingabefelder
verzeichnis_var = tk.StringVar()
name_var = tk.StringVar()
domain_var = tk.StringVar()
ip_var = tk.StringVar()

# Stil für die Widgets


widget_style = {
'bg': 'black',
'fg': 'lime',
'highlightbackground': 'lime',
'highlightcolor': 'lime',
'highlightthickness': 1,
'insertbackground': 'lime',
'font': ("Courier", 10),
'bd': 0
}

# Formular-Frame
form_frame = tk.Frame(main_frame, bg='black', padx=20, pady=20)
form_frame.pack(pady=(50, 20))

def create_field(label_text, variable, row, button=False):


frame = tk.Frame(form_frame, bg='black')
frame.grid(row=row, column=0, sticky="ew", pady=5)

label = tk.Label(frame, text=label_text, fg="lime",


bg='black', width=15, anchor="e",
font=("Courier", 10))
label.pack(side="left", padx=(0, 10))

entry = tk.Entry(frame, textvariable=variable, **widget_style, width=30)


entry.pack(side="left", fill="x", expand=True)

if button:
btn = tk.Button(frame, text="Durchsuchen", command=verzeichnis_waehlen,
bg='black', fg="lime", font=("Courier", 10),
highlightbackground="lime", highlightthickness=1,
activebackground='black', activeforeground="white",
bd=0)
btn.pack(side="left", padx=(10, 0))

create_field("Verzeichnis:", verzeichnis_var, 0, button=True)


create_field("Name:", name_var, 1)
create_field("Domain:", domain_var, 2)
create_field("IP-Adresse:", ip_var, 3)

# Such-Button
search_btn = tk.Button(form_frame, text="Suchen", command=durchsuchen,
bg='black', fg="lime", font=("Courier", 12, "bold"),
padx=20, pady=5, bd=0,
activebackground='black', activeforeground="white")
search_btn.grid(row=4, column=0, pady=(20, 0), sticky="ew")

# Ergebnisse-Frame
results_frame = tk.Frame(main_frame, bg='black', height=300)
results_frame.pack(fill="both", expand=True, padx=50, pady=(0, 50))

# Text-Widget
text_container = tk.Frame(results_frame, bg='black')
text_container.pack(fill="both", expand=True)

ausgabe_text = tk.Text(text_container, bg='black', fg="lime",


insertbackground="lime", wrap="word",
font=("Courier", 10), highlightthickness=0,
bd=0)
ausgabe_text.tag_config("highlight", foreground="lime", background='black')

scroll = tk.Scrollbar(text_container, command=ausgabe_text.yview)


ausgabe_text.config(yscrollcommand=scroll.set)

ausgabe_text.pack(side="left", fill="both", expand=True)


scroll.pack(side="right", fill="y")

# Funktion für Größenanpassung


def on_resize(event):
# Canvas-Größe
canvas_width = canvas.winfo_width()
canvas_height = canvas.winfo_height()

# Hintergrundbild skalieren
if bg_photo:
resized_bg = bg_image.resize((canvas_width, canvas_height), Image.LANCZOS)
new_bg = ImageTk.PhotoImage(resized_bg)
canvas.itemconfig(bg_id, image=new_bg)
canvas.image = new_bg # Referenz halten

# Hauptframe zentrieren
main_frame.update_idletasks()
frame_width = min(800, canvas_width - 100)
frame_height = main_frame.winfo_reqheight()

x = max(0, (canvas_width - frame_width) // 2)


y = max(50, (canvas_height - frame_height) // 4)

canvas.coords("main_frame", x, y)

# Frame-Größen anpassen
form_frame.config(width=frame_width)
results_frame.config(width=frame_width)

# Initiale Anpassung
canvas.bind("<Configure>", on_resize)

root.mainloop()

You might also like