I am creating a code editor and I want to create a minimap like other code editor have but I have no idea about how to create it in python tkinter
Asked
Active
Viewed 220 times
1 Answers
2
Text widgets can have peers - two or more widgets that share the same content. Just give the second text widget a tiny font.
Unfortunately, tkinter's support of peer widgets isn't complete, so it's best to create a helper class to do most of the work. I provided an example in this answer to the question How to enter text into two text widgets by just entring into same widget
Here's an example of how to use it:
import tkinter as tk
from tkinter.font import Font
class TextPeer(tk.Text):
"""A peer of an existing text widget"""
count = 0
def __init__(self, master, cnf={}, **kw):
TextPeer.count += 1
parent = master.master
peerName = "peer-{}".format(TextPeer.count)
if str(parent) == ".":
peerPath = ".{}".format(peerName)
else:
peerPath = "{}.{}".format(parent, peerName)
# Create the peer
master.tk.call(master, 'peer', 'create', peerPath, *self._options(cnf, kw))
# Create the tkinter widget based on the peer
# We can't call tk.Text.__init__ because it will try to
# create a new text widget. Instead, we want to use
# the peer widget that has already been created.
tk.BaseWidget._setup(self, parent, {'name': peerName})
root = tk.Tk()
text_font = Font(family="Courier", size=14)
map_font = Font(family="Courier", size=4)
text = tk.Text(root, font=text_font, background="black", foreground="white")
minimap = TextPeer(text, font=map_font, state="disabled",
background="black", foreground="white")
minimap.pack(side="right", fill="y")
text.pack(side="left", fill="both", expand=True)
root.mainloop()

Bryan Oakley
- 370,779
- 53
- 539
- 685
-
Sir thanks for solving my problem but one problem occurs that is my undo function is not working when ever I use minimap ,Sir how to fix it please – Coder Apr 18 '22 at 10:09
-
@Coder you have not provided any information on how your undo function works. – Billy Apr 27 '22 at 13:57
-
@Billy Sir, In this have provide information related it https://stackoverflow.com/questions/72028117/undo-function-of-text-widget-isnt-working-whenever-i-attach-minimap-with-it-in – Coder Apr 27 '22 at 14:03