簡體   English   中英

在 tkinter 畫布對象的查看區域周圍創建“障礙”

[英]Creating "barriers" around the viewing area of a tkinter canvas object

好的,所以我在這里有一種獨特的情況,所以請耐心等待。 我希望能夠在 tkinter 畫布對象的查看區域(用戶可見的畫布部分)周圍創建所謂的“障礙”。 例如,看看下面的截圖(基於最后的 MCVE):

問題

正如您在上圖中所看到的,當用戶到達終點時,線條當前會超出畫布的查看區域。 然而,這不是我想要的。 相反,我希望每當用戶到達畫布可見區域的末端時,“障礙”就會變熱,並且在接觸時發生回車,並且行從那里繼續。 因此,我真正想要的是:

應該發生什么

這是我用來截取上述屏幕截圖的 MCVE:

import tkinter as TK

xold = None
yold = None

class canvas(TK.Frame):
    def __init__(self, root, *args, **kwargs):
        # Initialize a tkinter frame widget
        TK.Frame.__init__(self, root, width = 800, height = 850, *args, **kwargs)
        self.root = self.winfo_toplevel()
        self.bg = "white"
        self.width, self.height = 850, 800
        self.canvwidth, self.canvheight = 10000, 10000
        # Set up the canvas and its corresponding scrollbars
        self.canvas = TK.Canvas(root, width=850, height=800,
                                 bg=self.bg, borderwidth=0, highlightthickness = 5, highlightbackground = 'brown', highlightcolor = 'brown')
        self.hscroll = TK.Scrollbar(root, command=self.canvas.xview,
                                    orient=TK.HORIZONTAL)
        self.vscroll = TK.Scrollbar(root, command=self.canvas.yview)
        self.canvas.configure(xscrollcommand=self.hscroll.set,
                               yscrollcommand=self.vscroll.set)
        self.rowconfigure(0, weight=1, minsize=0)
        self.columnconfigure(0, weight=1, minsize=0)
        # Add the scrollbars into the root window
        self.canvas.grid(padx=1, pady=1, row=0,
                column=0, rowspan=1, columnspan=1, sticky = 'news')
        self.vscroll.grid(padx=1, pady=1, row=0,
                column=1, rowspan=1, columnspan=1, sticky='news')
        self.hscroll.grid(padx=1, pady=1, row=1,
                column=0, rowspan=1, columnspan=1, sticky='news')
        # Call the `reset` method of the canvas class
        self.reset()
        # Bind the `line` method to the 'l' key of the users keyboard (as an example of what I want)
        self.root.bind('<l>', self.line)

    def reset(self, canvwidth=None, canvheight=None, bg = None):
        ###############################################################################################################################
        # This adds the scrollbars themselves to the canvas and adapts them to the canvas's size (in this case, 10000 x 10000 pixels) #
        ###############################################################################################################################

        if canvwidth:
            self.canvwidth = canvwidth
        if canvheight:
            self.canvheight = canvheight
        if bg:
            self.bg = bg
        self.canvas.config(bg=bg,
                        scrollregion=(-self.canvwidth//2, -self.canvheight//2,
                                       self.canvwidth//2, self.canvheight//2))
        self.canvas.xview_moveto(0.5*(self.canvwidth - self.width + 30) /
                                                               self.canvwidth)
        self.canvas.yview_moveto(0.5*(self.canvheight- self.height + 30) /
                                                              self.canvheight)

    def line(self, event):
        ########################################################################################################
        # Create a short, horizontal, black line on every press of the user's 'l' key (as an example to go by) #
        ########################################################################################################
        global xold, yold
        if xold != None and yold != None:
            pass
        else:     
            xold, yold = 0, 0
        self.canvas.create_line(xold, yold, xold+30, yold, smooth = TK.TRUE, width = 1, capstyle = TK.ROUND, joinstyle = TK.ROUND, fill = 'black')
        xold = xold+30
        yold = yold

if __name__ == '__main__':
    # Create a window, and provide that window to the canvas class as the root window
    root = TK.Tk()
    root.geometry('900x850')
    canvas(root)
    root.mainloop()

是否可以使用 tkinter 將此功能添加到上面的 MCVE 中? 如果是這樣,我將如何開始嘗試實施它?

我不確定您實際要做什么(特別是在您提供帶有滾動條的非常大的畫布時試圖限制顯示區域中的繪圖)。

對於最簡單的情況,您只需要一個綁定值並針對它測試xold

if xold > 440:
    xold = -410
    yold += 30

如果要考慮當前顯示的區域,則必須結合來自 canvas scrollregionxview方法的信息。 第一個返回畫布的邊界,並在滾動區域中形成顯示區域的相對位置。

    scroll = list(map(int,self.canvas["scrollregion"].split()))
    xview = self.canvas.xview()
    leftbound = scroll[0] + xview[1] * (scroll[2]-scroll[0])
    if xold > leftbound:
        rightbound = scroll[0] + xview[0] * (scroll[2]-scroll[0])
        xold = rightbound
        yold += 30

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM