a basic tkinter attempt that uses enter to handle the input

This commit is contained in:
Joost Agterhoek 2024-08-08 16:45:44 +02:00
parent 89f1312cb9
commit b46e9413ad

108
tkinter-enter.py Normal file
View File

@ -0,0 +1,108 @@
# From: https://www.geeksforgeeks.org/how-to-bind-the-enter-key-to-a-tkinter-window/
# importing tkinter
import tkinter as tk
from generic import sanitize, type_hosts_listed, type_hosts_organized, ip_lookup, domain_lookup, spf_dmarc
# function to run on pressing
# 'ENTER' key from keyboard.
def key_handler_function(event):
inputs = entry.get()
sanitized = sanitize.strip_and_list(inputs)
typed = type_hosts_listed.type_hosts(sanitized)
textbox.insert('end', typed)
listed = type_hosts_organized.type_hosts(sanitized)
# IPs = listed['IPs']
# below works for updating a global variable (here)...
var.set(listed['IPs'])
ip_listbox.delete(0, 'end')
domain_listbox.delete(0, 'end')
url_listbox.delete(0, 'end')
email_listbox.delete(0, 'end')
for IP in listed['IPs']:
hostname = ip_lookup.get_hostname(IP)
output = "{} => {}".format(IP, hostname[0])
ip_listbox.insert('end', output)
for domain in listed['domains']:
ip = domain_lookup.get_ip_address(domain)
output = "{} => {}".format(domain, ip)
domain_listbox.insert('end', output)
for URL in listed['URLs']:
url_listbox.insert('end', URL)
for email in listed['emails']:
dmarc, spf = spf_dmarc.lookup(email)
output_dmarc = "{} => {}".format(email, dmarc)
output_spf = "{} => {}".format(email, spf)
email_listbox.insert('end', output_dmarc, output_spf)
def test_button_function():
# testing quoting and comma separating hosts, IPs for now, from global StringVar var
textbox.delete(1.0, 'end')
IPs = ', '.join([f'"{host}"' for host in var.get()])
textbox.insert('end', IPs)
print("var is : {}".format(var.get()))
# ... and printing an updated global variable here!
# main application window
root = tk.Tk()
root.title("Enter IP addresses, URLs, domains, email addresses")
root.geometry("900x600")
root.resizable(0,0)
# configure the grid
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
root.columnconfigure(2, weight=1)
root.columnconfigure(3, weight=1)
#Listing stuff
# Gotta try out: https://www.pythontutorial.net/tkinter/tkinter-listbox/
# creating widgets
# try to use grid instead of pack: https://www.geeksforgeeks.org/python-grid-method-in-tkinter/
entry = tk.Entry(root, text="Put in IP addresses, URLs, domains, email addresses")
textbox = tk.Text(root, height=8)
# textbox.insert('1.0', test)
entrylabel = tk.Label(root, text="Copy-paste or type and Enter :D")
ip_label = tk.Label(root, text="IP and hostname")
ip_listbox = tk.Listbox(root)
domain_label = tk.Label(root, text="domain => IP")
domain_listbox = tk.Listbox(root)
url_label = tk.Label(root, text="URL => IP")
url_listbox = tk.Listbox(root)
email_label = tk.Label(root, text="email address => DMARC/SPF")
email_listbox = tk.Listbox(root)
var = tk.Variable()
entrylabel.grid(column=1, row=0, columnspan=2, sticky='nsew')
entry.grid(column=0, row=1, columnspan=4, sticky='nsew')
ip_label.grid(column=0, row=2, columnspan=2, sticky='nsew')
ip_listbox.grid(column=0, row=3, columnspan=2, sticky='nsew')
domain_label.grid(column=2, row=2, columnspan=2, sticky='nsew')
domain_listbox.grid(column=2, row=3, columnspan=2, sticky='nsew')
url_label.grid(column=0, row=4, columnspan=2, sticky='nsew')
url_listbox.grid(column=0, row=5, columnspan=2, sticky='nsew')
email_label.grid(column=2, row=4, columnspan=2, sticky='nsew')
email_listbox.grid(column=2, row=5, columnspan=2, sticky='nsew')
# label.grid(column=1, row=2)
# textbox.grid(column=1, row=3)
#Trying out buttons
# Try to create a copy-paste button: https://www.delftstack.com/howto/python/python-copy-to-clipboard/#copy-text-to-clipboard-in-python-using-the-tkinter-module
test_button = tk.Button(root, text="comma-separate and add quotes", command=test_button_function)
# test_button.grid(column=2, row=5, columnspan=2)
# Binding ENTER key to function
entry.bind("<Return>", key_handler_function)
# run the application
root.mainloop()
# Instead of a label showing the output, use a text box: https://pythonguides.com/python-tkinter-text-box/ -> DONE
# Implement a copy paste button: https://stackoverflow.com/a/71357987
# Try out ways to sanitize input, f.e. extract email addresses like in Cyberchef: https://github.com/gchq/CyberChef/blob/master/src/core/operations/ExtractEmailAddresses.mjs
# Another simpler way would be to first account for special characters, which are not allowed in email addresses: https://stackoverflow.com/questions/2049502/what-characters-are-allowed-in-an-email-address