我一直在开发一个 GUI 程序来清理 URL 和 IP 地址。目前,我想清理输入和打印结果的“打印”按钮不起作用。我的其他功能按照我想要的方式工作,这些功能已经过测试。
在printOut()函数中,该代码片段在我正在使用的先前 CLI 版本中工作。
我已经包含了我的 GUI 界面的屏幕截图,以显示我想要的结果。对我的代码的任何帮助将不胜感激。
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()