如何通过 Tkinter 在 Canvas 中同时显示重叠的标签和文本?
我想在 Tkinter 的 Canvas 中同时显示重叠的标签和文本。 这是我的数据。
import tkinter as tk # python 3
def populate(frame):
'''Put in some fake data'''
for row in range(100):
tk.Label(frame, text="%s" % row, width=3, borderwidth="1", bg="red",
relief="solid").grid(row=row, column=0)
t="this is the second column for row %s" %row
tk.Label(frame, text=t,bg="pink").grid(row=2*row, column=1)
def onFrameConfigure(canvas):
'''Reset the scroll region to encompass the inner frame'''
canvas.configure(scrollregion=canvas.bbox("all"))
root = tk.Tk()
canvas = tk.Canvas(root, borderwidth=0,bg="blue")
frame = tk.Frame(canvas,bg="yellow")
vsb = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((0,0), window=frame, anchor="nw")
frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))
populate(frame)
canvas.create_text(250,300,text="10kV/35kV")
root.mainloop()
但现在重叠的文本(“10kV/35kV”)被隐藏,如图所示。
有没有办法显示整个文本,即使它与标签重叠?
I want to show overlapped Label and text simultaneously in Canvas by Tkinter.
Here are my data.
import tkinter as tk # python 3
def populate(frame):
'''Put in some fake data'''
for row in range(100):
tk.Label(frame, text="%s" % row, width=3, borderwidth="1", bg="red",
relief="solid").grid(row=row, column=0)
t="this is the second column for row %s" %row
tk.Label(frame, text=t,bg="pink").grid(row=2*row, column=1)
def onFrameConfigure(canvas):
'''Reset the scroll region to encompass the inner frame'''
canvas.configure(scrollregion=canvas.bbox("all"))
root = tk.Tk()
canvas = tk.Canvas(root, borderwidth=0,bg="blue")
frame = tk.Frame(canvas,bg="yellow")
vsb = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((0,0), window=frame, anchor="nw")
frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))
populate(frame)
canvas.create_text(250,300,text="10kV/35kV")
root.mainloop()
But now the overlapped text ("10kV/35kV") is hidden,as shown in this figure.
Is there any way to show the whole text even if it is overlapped with labels?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此代码演示了如何将
winA
与winB
叠加。将
canvas.create_text(250,300,text="10kV/35kV")
替换为此代码段。This code demonstrates how to overlay
winA
withwinB
.Replace
canvas.create_text(250,300,text="10kV/35kV")
with this snippet.