Search code examples
python-3.xtkintertkinter-text

Taking input from tkinter.Text and printing the results


I've been working on a GUI program to sanitise URLs and IP addresses. Currently the "Print" button which I want to sanitise input and print results, is not working. My other functions work how I want them to, which have been tested.

In the printOut() function, that code snippet worked in a previous CLI version I was working on.

I've included a screenshot of my GUI interface to show what I want results to look like. Any help on my code would be greatly appreciated.

import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog

# help information -- works
def help_info():
    h = messagebox.showinfo("Help", "This program is used to defang/sanitise URLs and IP addresses.\n"
                                    "\nEnter a URL or IP address into the top input field and have it printed to the 'Results' output or, save it to a file.")

# Print sanitised results out
# not working
def printOut():
    url = inputTextbox.get(1.0, "end-1c")

    if "http" in url:
        url = url.replace("http", "hxxp")
        url = url.replace(":", "[:]")
        url = url.replace(".", "[.]")

    elif "https" in url:
        url = url.replace("https", "hxxps")
        url = url.replace(":", "[:]")
        url = url.replace(".", "[.]")

    elif "http" not in url:
        url = url.replace(".", "[.]")

    elif "https" not in url:
        url = url.replace(".", "[.]")
    return(url)

# save output to a file -- works
def save_as():
    save_output = filedialog.asksaveasfile()


# quit the program -- works
def quit_program():
    are_you_sure = messagebox.askquestion("Quit", "Are you sure?")
    if are_you_sure == 'yes':
        mainWindow.destroy()
    else:
        mainWindow.mainloop()


# Create mainWindow, not resizeable
mainWindow = tk.Tk()
mainWindow.title("Defang")
mainWindow.geometry("585x355-730-400")
mainWindow.resizable(False, False)

# label for user input
input_label = tk.Label(mainWindow, text="Enter URL(s) and or IP Address(s)", font="Arial 9")
input_label.place(x=7, y=5)

# input textbox
inputTextbox = tk.Text(mainWindow, width=70, height=8)
inputTextbox.place(x=10, y=25)

# label for output
output_label = tk.Label(mainWindow, text="Results", font="Arial 9")
output_label.place(x=7, y=165)

# results textbox
outputTextbox = tk.Text(mainWindow, width=70, height=8)
outputTextbox.place(x=10, y=185)

# help button
help_button = tk.Button(mainWindow, text="Help", padx=20, pady=0, command=help_info)
help_button.place(x=100, y=325)

# print button
print_button = tk.Button(mainWindow, text="Print", padx=20, pady=0, command=printOut)
print_button.place(x=200, y=325)

# save button
save_button = tk.Button(mainWindow, text="Save", padx=20, pady=0, command=save_as)
save_button.place(x=300, y=325)

# exit button
exit_button = tk.Button(mainWindow, text="Exit", padx=20, pady=0, command=quit_program)
exit_button.place(x=400, y=325)

# run the program
mainWindow.mainloop()

GUI


Solution

  • The printOut function is not working because after replacing the URL you are not inserting it in the outputTextbox. After all conditions in printout function, you should replace return url code from this.

    outputTextbox.insert(1.0, url)