我正在创建一个代码编辑器,我想像其他代码编辑器一样创建一个微图,但是我不知道如何在python tkinter中创建它。
我想在这张图中创建一个小地图
这就是我所创造的:-
发布于 2022-04-16 07:34:40
文本小部件可以有对等点--两个或多个共享相同内容的小部件。只需给第二个文本小部件一个小字体即可。
不幸的是,tkinter对对等部件的支持还没有完成,所以最好创建一个帮助类来完成大部分工作。我在this answer中给出了一个How to enter text into two text widgets by just entring into same widget问题的例子
下面是一个如何使用它的示例:
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()
https://stackoverflow.com/questions/71894385
复制相似问题