简体   繁体   English

Python Tkinter Canvas 的边框与由 `create_window' 创建的窗口重叠

[英]Python Tkinter Canvas' border overlap with window create by `create_window'

I am using tkinter to make a scrolled canvas.我正在使用 tkinter 制作滚动画布。 When I create a window by create_window link with a frame which is bigger then the canvas, the window will overlap with the border of canvas.当我通过create_window链接创建一个具有比画布大的框架的窗口时,窗口将与画布的边框重叠。

import Tkinter as tk


class ScrollCanvas(object):
    """A widget to show table in frame"""
    def __init__(self, parent=None):
        self.parent = parent
        self.frame = tk.Frame(self.parent, bd=3,
                              relief=tk.GROOVE)
        self.frame.pack(fill=tk.BOTH, expand=1)

        self.frame.grid_rowconfigure(0, weight=1)
        self.frame.grid_columnconfigure(0, weight=1)

        self.xscrollbar = tk.Scrollbar(self.frame, orient=tk.HORIZONTAL)
        self.yscrollbar = tk.Scrollbar(self.frame, orient=tk.VERTICAL)
        self.canvas = tk.Canvas(self.frame, bg='#006666',
                                bd=10, relief=tk.GROOVE,
                                xscrollcommand=self.xscrollbar.set,
                                yscrollcommand=self.yscrollbar.set)

        self.xscrollbar.grid(row=1, column=0, sticky=tk.W+tk.E)
        self.yscrollbar.grid(row=0, column=1, sticky=tk.N+tk.S)
        self.canvas.grid(row=0, column=0, sticky=tk.W+tk.E+tk.N+tk.S)

        self.xscrollbar.config(command=self.canvas.xview)
        self.yscrollbar.config(command=self.canvas.yview)

        self.table = tk.Frame(self.canvas, bd=5, relief=tk.SOLID,
                              width=400, height=200)

        self.canvas.create_window(0, 0, anchor=tk.CENTER, window=self.table)
        self.canvas.update_idletasks()
        self.canvas.config(scrollregion=self.canvas.bbox(tk.ALL))


def main():
    app = tk.Tk()
    app.geometry('400x300')
    frame = tk.Frame(app, width=50, height=50)
    frame.propagate(0)
    frame.pack(fill=tk.BOTH, expand=1)
    mytable = ScrollCanvas(parent=frame)
    app.mainloop()

main()

The result is like this:(the red box)结果是这样的:(红框)

在此处输入图片说明

Does anyone know how to fix this?有谁知道如何解决这个问题?

Oddly, the canvas border is part of the drawing space, so anything drawn on the canvas has the potential to overlap the borders.奇怪的是,画布边框是绘图空间的一部分,因此在画布上绘制的任何内容都有可能与边框重叠。

The solution that I use is to remove the border from the canvas, and then put the canvas inside a frame that has a border.我使用的解决方案是从画布上移除边框,然后将画布放入具有边框的框架内。 Visually it looks the same, but because the visible border belongs to another widget it is impossible for items in the canvas to overlap it.从视觉上看,它看起来是一样的,但因为可见边框属于另一个小部件,画布中的项目不可能与它重叠。

You can solve that problem by setting the state to be hidden and when it's needed to set it back to normal like this您可以通过将状态设置为隐藏以及何时需要像这样将其设置回正常来解决该问题

import tkinter

...
...

canvas = tkinter.Canvas(self, width=100, height=100, bd=0, highlightthickness=0)
canvas.pack()

btn = tkinter.Button(text="Click me", command=print)

win = canvas.create_window(10, 10, anchor=tkinter.NW, window=btn)

canvas.itemconfigure(win, state=tkinter.HIDDEN)

...
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM