-1

i'm working to make an ide for python with tkinter,i want to add highlighting for python

import tkinter as tk

root = tk.Tk()
root.title('Python ide')

text = tk.Text(root)
text.pack()


root.mainloop()
johmin
  • 29
  • 2
  • Does this answer your question? [How to change the color of certain words in the tkinter text widget?](https://stackoverflow.com/questions/14786507/how-to-change-the-color-of-certain-words-in-the-tkinter-text-widget) – PCM Sep 08 '21 at 12:11
  • 2
    There are already a few questions about syntax highlighting with tkinter, you can use the search bar from StackOverflow to find them https://stackoverflow.com/search?q=%5Btkinter%5D+syntax+highlighting. Have a look at them and try to implement your own solution. If you get stuck, update this question with the details of your attempt. – j_4321 Sep 08 '21 at 12:26
  • 1
    Try looking at `idlelib` – TheLizzard Sep 08 '21 at 12:26
  • @TheLizzard can you explain more,how to do that? – johmin Sep 08 '21 at 13:39
  • @johmin Try: `from idlelib.colorizer import _color_delegator; from tkinter import Tk; _color_delegator(Tk())`. If you want to know how it works, just look at the source code. – TheLizzard Sep 08 '21 at 13:59
  • @TheLizzard do you know how to do that with pygments please – johmin Sep 08 '21 at 16:42
  • @johmin Nope. I have only used `idlelib` for the code highlighting. – TheLizzard Sep 08 '21 at 16:48
  • @TheLizzard Thank you so much – johmin Sep 08 '21 at 17:09

1 Answers1

2

I found this solution

import idlelib.colorizer as ic
import idlelib.percolator as ip
import tkinter as tk

root = tk.Tk()
root.title('Python Syntax Highlighting')

text = tk.Text(root)
text.pack()

cdg = ic.ColorDelegator()




cdg.tagdefs['COMMENT'] = {'foreground': '#FF0000', 'background': '#FFFFFF'}
cdg.tagdefs['KEYWORD'] = {'foreground': 'purple', 'background': '#FFFFFF'}
cdg.tagdefs['BUILTIN'] = {'foreground': '#7F7F00', 'background': '#FFFFFF'}
cdg.tagdefs['STRING'] = {'foreground': '#7F3F00', 'background': '#FFFFFF'}
cdg.tagdefs['DEFINITION'] = {'foreground': '#007F7F', 'background': '#FFFFFF'}

ip.Percolator(text).insertfilter(cdg)

root.mainloop()
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
johmin
  • 29
  • 2