如何通过 Tkinter 在 Canvas 中同时显示重叠的标签和文本?

发布于 2025-01-09 02:08:29 字数 1234 浏览 0 评论 0原文

我想在 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”)被隐藏,如图所示。

1

有没有办法显示整个文本,即使它与标签重叠?

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.

1

Is there any way to show the whole text even if it is overlapped with labels?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

執念 2025-01-16 02:08:29

此代码演示了如何将 winAwinB 叠加。

canvas.create_text(250,300,text="10kV/35kV") 替换为此代码段。

message = tk.Label(canvas, text = "10kV/35kV", bg = 'blue', fg = 'white')
winB = canvas.create_window(80, 21, window = message, anchor = tk.NW)

This code demonstrates how to overlay winA with winB.

Replace canvas.create_text(250,300,text="10kV/35kV") with this snippet.

message = tk.Label(canvas, text = "10kV/35kV", bg = 'blue', fg = 'white')
winB = canvas.create_window(80, 21, window = message, anchor = tk.NW)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文