[求助]text控件对指定内容染色
是这样的,我想要做一个html编辑器。然后我现在想实现一个部分关键字变色的效果(和Python IDLE输入print()变成橙色的效果一样。),可是我做不出来。还请各位大佬指导:源代码:
程序代码:
import about_file as f from tkinter import * import webbrowser as webo def close(*args): tk.destroy() def iconify(*args): tk.iconify() def z(*args): tk.overrideredirect(True) def o(*args): tk.overrideredirect(False) class commands(): def __init__(self): self.file_name = f.getFileName('index.html') def save(self): with open(self.file_name, 'w') as file: global codes file.write(codes.get("1.0",END)) def run(self): self.save() webo.open('file://'+self.file_name) def get(self): with open(self.file_name, 'r') as file: code = file.read() if code == '': return '' else: return code tk = Tk() c = commands() # 获取屏幕宽 screen_width = tk.winfo_screenwidth() # 获取屏幕高 screen_height = tk.winfo_screenheight() tk.title('HTML编译器') tk.geometry('%sx%s' % (screen_width, screen_height)) tk.overrideredirect(True) tk.config(bg='white') btn1 = Button(tk, text = '保存', relief='flat', font=('微软雅黑', 8, 'bold'), bg='white', command = c.save) btn1.grid(row = 0, column = 0) btn2 = Button(tk, text = '运行', relief='flat', font=('微软雅黑', 8, 'bold'), bg='white', command = c.run) btn2.grid(row = 0, column = 1) btn3 = Button(tk, text = '关闭', relief='flat', font=('微软雅黑', 8, 'bold'), bg='white', command=close) btn3.grid(row = 0, column = 2) btn4 = Button(tk, text = '最小化', relief='flat', font=('微软雅黑', 8, 'bold'), bg='white', command=iconify) btn4.grid(row = 0, column = 3) btn5 = Button(tk, text = '恢复标题栏', relief='flat', font=('微软雅黑', 8, 'bold'), bg='white', command=o) btn5.grid(row = 0, column = 5) btn5 = Button(tk, text = '全屏化', relief='flat', font=('微软雅黑', 8, 'bold'), bg='white', command=z) btn5.grid(row = 0, column = 4) tk.update() codes = Text(tk, font=('微软雅黑', 10, 'bold'), bg='Cyan', fg='Green') codes.place(x = 0, y = 25, width=screen_width, height=screen_height) codes.insert(END,c.get()) codes.insert(END,'你好') tk.mainloop()