从 tkinter.Text 获取输入并打印结果
Taking input from tkinter.Text and printing the results
我一直在开发一个 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()
printOut
函数不起作用,因为在替换 URL 后,您没有将它插入 outputTextbox
。在 printout
函数中的所有条件之后,您应该从中替换 return url
代码。
outputTextbox.insert(1.0, url)
我一直在开发一个 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()
printOut
函数不起作用,因为在替换 URL 后,您没有将它插入 outputTextbox
。在 printout
函数中的所有条件之后,您应该从中替换 return url
代码。
outputTextbox.insert(1.0, url)