我正在做一個簡單的編程專案,決議一個寫在文本編輯器中的Java程式。文本編輯器中的程式由一個使用tinkter庫的python程式來處理。
下面是一個java程式的例子
public class TestMax {
public static void main(String args[]){
System.out.println("Hello World"/span>)。
}
}
下面是實際的結果
public class TestMax{
public static <span class="keyword">void</span> main(String args[]){
System.out.println("Hello World")。
}
}
下面是預期的輸出
<span class="keyword"> public< /span> <span class="keyword"> class</span> TestMax {
<span class="keyword"> public</span> <span class="keyword"> static</span> <span class="keyword">void</span> main(String args[]){
System.out.println("Hello World")。
}
}
下面是python程式
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
import re
keywords = ["public","class","static", "void", "int","if","else","return"]
def open_file()。
""打開一個檔案進行編輯。""
filepath = askopenfilename(
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*") ]
)
if not filepath:
return return
txt_edit.delete(1.0, tk.END)
with open(filepath, "r") as input_file:
text = input_file.read()
txt_edit.insert(tk.END, text)
window.title(f "Simple Text Editor - {filepath}" )
def save_file()。
""將當前檔案保存為一個新檔案。""
filepath = asksaveasfilename(
defaultextension="txt"/span>。
filetypes=[("文本檔案", "*.txt"), ("所有檔案", "*.*") ]。
)
if not filepath:
return not filepath.
with open(filepath, "w") asoutput_file:
text = txt_edit.get(1.0, tk.END)
output_file.write(text)
window.title(f "Simple Text Editor - {filepath}")
def print_html()。
""Print html""
text = txt_edit.get(1.0, tk.END)
for line in text.split("
")。)
for word in line.split(" ") 。
if word in keywords:
text_after = re.sub(word, '<span class="keyword">' word '< /span> ', text)
print(text_after)
window = tk.Tk()
window.title("簡單文本編輯器")
window.rowconfigure(0, minsize=800, weight= 1)
window.columnconfigure(1, minsize=800, weight=1)
txt_edit = tk.Text(window)
fr_buttons = tk.Frame(window, relief=tk.RAISED, bd=2)
btn_open = tk.Button(fr_buttons, text="Open"/span>, command=open_file)
btn_save = tk.Button(fr_buttons, text="保存為...", command=save_file)
btn_print_html = tk.Button(fr_buttons, text="Print as html"/span>, command=print_html)
btn_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5)
btn_save.grid(row=1, column=0, sticky="ew", padx=5)
btn_print_html.grid(row=2, column=0, sticky="ew", padx=5)
fr_buttons.grid(row=0, column=0, sticky="ns")
txt_edit.grid(row=0, column=1, sticky="nsew")
window.mainloop()
問題出在這個方法中:
def print_html()。
""Print html"".
text = txt_edit.get(1.0, tk.END)
for line in text.split("
")。)
for word in line.split(" ") 。
if word in keywords:
text_after = re.sub(word, '<span class="keyword">' word '< /span> ', text)
print(text_after)
問題是,關鍵詞只被替換了一次。理想情況下,我想用<span class="keyword">word</span>替換所有的關鍵詞。如果有任何幫助,我們將不勝感激。謝謝!
uj5u.com熱心網友回復:
你沒有保存你的修改,并不斷重寫text_after
re在這里是不行的,因為你會得到你絕對不想要的回圈(你在添加<span class...,而class是你的關鍵詞之一),所以你最好寫一個全新的文本
def print_html() 。
""Print html"".
text = txt_edit.get(1.0, tk.END)
new_text=[]
for line in text.split("
")。)
line_text = []
for word in line.split(" ") 。
if word in keywords:
line_text.append('<span class="keyword">' word '</span> ' )
else:
line_text.append(word)
new_text.append(' '/span>.join(line_text))
new_text = '
'.join(new_text)
print(new_text)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/307544.html
標籤:
下一篇:如何獲得平衡括號組的內容
