'How to create minmap in like other modern code editor in python tkinter?
Solution 1:[1]
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bryan Oakley |



