使用滚动条时文本小部件内容覆盖的 tkinter 选项卡 [英] tkinter Tab covered by the text widget content when using scrollbar

查看:49
本文介绍了使用滚动条时文本小部件内容覆盖的 tkinter 选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请我的代码有一个小问题,我希望你们能帮助我,伙计们,我正在使用滚动条在我的文本小部件中滚动图像,但问题是当我滚动时标签窗口被图像覆盖.

please i have a little problem white my code and i hope you can help me guys, i'm using a scrollbar to scroll over images in my text widget, but the problem is that the tab window is covered by the images when i scroll.

最后一张图片显示了问题,我希望标签在滚动时保持显示,图像应留在标签内.这是我使用的代码:

this last picture shows the problem, and i want the tabs to remain showing when i scroll and the images should stay inside the tab. here's the code i used:

import tkinter as tk
from PIL import ImageTk, Image
from os import listdir
from os.path import isfile, join
from tkinter import ttk

def getPaths():
    path = "C://Users/poste/Desktop/trainCascade/p"
    onlyfiles = [f for f in listdir(path) if isfile(join(path, f))]
    return onlyfiles

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        style = ttk.Style()
        style.theme_create('pastel', settings={
            ".": {
                "configure": {
                    "background": 'white',  # All except tabs
                    "font": 'red'
                }
            },
            "TNotebook": {
                "configure": {
                    "background": '#848a98',  # Your margin color
                    "tabmargins": [2, 5, 0, 0],  # margins: left, top, right, separator
                }
            },
            "TNotebook.Tab": {
                "configure": {
                    "background": '#d9ffcc',  # tab color when not selected
                    "padding": [10, 2],
                    "font": "white"
                },
                "map": {
                    "background": [("selected", '#ccffff')],  # Tab color when selected
                    "expand": [("selected", [1, 1, 1, 0])]  # text margins
                }
            }
        })

    style.theme_use('pastel')

    tabControl = ttk.Notebook(self)

    tab1 = ttk.Frame(tabControl)
    tab2 = ttk.Frame(tabControl)

    tabControl.add(tab1, text='Tab 1')
    tabControl.add(tab2, text='Tab 2')
    tabControl.pack(expand=1, fill="both")

    text = tk.Text(tab1, wrap="none")
    vsb = tk.Scrollbar(tab1, orient="vertical", command=text.yview)
    text.configure(yscrollcommand=vsb.set)
    vsb.pack(side="right", fill="y")
    text.pack(fill="both", expand=True)

    img = []
    resized = []
    final = []
    label = []
    flag = 0
    for i in range(len(getPaths())):
        img.append(Image.open("C://Users/poste/Desktop/trainCascade/p/"+getPaths()[i]))
        resized.append(img[i].resize((107, 80), Image.ANTIALIAS))
        final.append(ImageTk.PhotoImage(resized[i]))

        label.append(tk.Label(image=final[i], bg='white'))
        label[i].image = final[i]
        text.window_create("end", window=label[i])
        flag+=1
        if flag==5:
            text.insert("end", "\n")
            flag=0

    text.configure(state="disabled")

if __name__ == "__main__":
    root = tk.Tk()
    root.geometry("575x400")
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

推荐答案

这个问题似乎是由于在创建带有图像的标签时缺少 master 参数造成的.

The problem seems to be caused by the lack of master argument when creating those labels with the images.

作为一个最小的工作示例:

As a minimal working example:

import tkinter as tk

root = tk.Tk()

label = tk.Label(root, text="If you scroll down the text box, this will " \
                            "be covered.", anchor="w")
label.pack(fill="x")

text = tk.Text(root)
text.pack()

for i in range(50):
    # Adding `text` as the first parameter here solves the problem
    label = tk.Label(text=f"Text label number {i}")
    text.window_create("end", window=label)
    text.insert("end", "\n")

root.mainloop()

当您不指定 master 参数时,tkinter 使用您为 master 创建的第一个窗口.问题在于文本小部件告诉 Label 绘制自身并给出超出其边界的坐标.Label 检查它是否在它的母版边框内,但由于它的母版不是文本小部件,因此可以在其他小部件上自由绘制.我认为这是 tcl 中的一个错误,tkinter 使用的库.

When you don't specify the master argument, tkinter uses the first window that you created for the master. The problem is that the text widgets tells the Label to draw itself giving coordinates that are outside of it's borders. The Label check if it's inside it's master's border but as it's master isn't the text widget it is free to draw over your other widgets. I think it's a bug in tcl, the library that tkinter uses.

修复:创建小部件时始终传入 master 参数.

Fix: Always pass in the master argument when creating widgets.

这篇关于使用滚动条时文本小部件内容覆盖的 tkinter 选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆