我是 Tkinter 的新手,我正在尝试构建一个注释工具,其中我希望用户选择的文本在按下键盘键时以某种颜色突出显示。到目前为止,这是我的代码。
from tkinter import *
import tkinter as tk
from tkinter import Frame, Menu
from tkinter import ttk
class StartPage(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.results_field = tk.Text(self)
self.results_field.insert('insert','sometext'*100)
self.results_field.config(height=10,spacing1=5,yscrollcommand=True,wrap='word')
self.results_field.tag_configure("start", background="black",foreground='red')
self.results_field.bind('<KeyPress>', self.onKeyPress)
self.results_field.pack()
def onKeyPress(self,event):
self.results_field.tag_add("start", "sel.first", "sel.last")
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(StartPage)
def switch_frame(self, frame_class):
new_frame = frame_class(self)
self._frame = new_frame
self._frame.pack()
def main():
app=App()
app.geometry("1000x500")
app.mainloop()
if __name__ == '__main__':
main()
我发布了整个代码,以免我在其他任何地方出错。任何帮助将不胜感激。提前致谢 :)