Tkinter Canvas Postscript没有捕获屏幕框架外的Window元素

时间:2016-11-29 12:50:47

标签: python canvas tkinter postscript

正如标题所说,当尝试使用Postscript保存Canvas时,它可以正常使用所有非窗口元素(rects,ovals等...),并且它可以完美地捕获当我推送时当前屏幕上的窗口元素按钮。但是当时屏幕外没有任何窗口元素。

这个问题看起来很随意我想知道是否有解决方案,希望有人在那里找到了解决方法。

以下是一些示例代码,我将简化以解释确切的问题:

#!/usr/bin/python3
#
# This file is intended as a simplified example for Stack Overflow.
# The original program is far greater and is a writing tool for branching dialogue, much like Twine.

from tkinter import Tk, Canvas, Frame, Text, Label

class Canv(Canvas):
    def __init__(self, parent):
        """Simple Canvas class."""
        Canvas.__init__(self, parent)
        self.parent = parent
        self.config(background="white", width=960, height=640)
        self.num = 1
        self.pack()
        self.bindings()

    def bindings(self):
        """All the button bindings."""
        self.bind("<Button-1>", self.add_window)
        self.bind("<ButtonPress-2>", self.mark)
        self.bind("<ButtonRelease-2>", self.drag)
        self.bind("<Button-3>", self.take_ps)

    def add_window(self, e):
        """Here I add the Label as a Canvas window.
           And include an Oval to mark its location.
        """
        text = "Textwindow {}".format(self.num)
        self.num += 1
        window = TextWindow(self, text)
        pos = (self.canvasx(e.x), self.canvasy(e.y))
        self.create_window(pos, window=window)
        bbox = (pos[0]-50, pos[1]-50, pos[0]+50, pos[1]+50)
        self.create_oval(bbox, width=3, outline="green")

    def mark(self, e):
        """Simple Mark to drag method."""
        self.scan_mark(e.x, e.y)

    def drag(self, e):
        """This drags, using the middle mouse button, the canvas to move around."""
        self.scan_dragto(e.x, e.y, 5)

    def take_ps(self, e):
        """Here I take a .ps file of the Canvas.
           Bear in mind the Canvas is virtually infinite, so I need to set the size of the .ps file
           to the bounding box of every current element on the Canvas.
        """
        x1, y1, x2, y2 = self.bbox("all")
        self.postscript(file="outfile.ps", colormode="color", x=x1, y=y1, width=x2, height=y2)
        print("Writing file outfile.ps...")

class TextWindow(Frame):
    def __init__(self, parent, text):
        """Very simple label class.
           Might have been overkill, I originally intended there to be more to this class,
            but it proved unnecesary for this example.
        """
        Frame.__init__(self, parent)
        self.pack()
        self.label = Label(self, text=text)
        self.label.pack()



if __name__ == "__main__":   #<---Boilerplate code to run tkinter.
    root = Tk()
    app = Canv(root)
    root.mainloop()

This is an example .jpg based on the postscript.

从图像中可以看出,右侧的所有绿色圆圈都有完整的窗口标签。那么所有的绿色圆圈都应该有它们,并且在程序中它们工作得很好,只是没有在后记中显示。是的,当我点击take_ps按钮时,我的屏幕位于右侧圆圈上。

至于替代方案,我需要画布可拖动,我需要它来扩展,可能在两个方向都有很大的距离。而且我不能将文本直接放在画布上,因为它会占用太多空间。它的目的是拥有文本字段,而不仅仅是画布上窗口中的标签(这个例子的代码太多了),我在窗口中需要文本的原因,而不是直接在屏幕上,文本可能很容易它应该占用更多的空间。我需要画布来显示文本字段之间的关系,以及包含要编辑的文本的文本窗口,不一定是完整显示。正如它所说,我正在为游戏制作分支对话工具,就像Twine一样。

1 个答案:

答案 0 :(得分:0)

我也遇到了这个问题。我能够临时配置画布以匹配输出图像的大小。完成创建后记文件后,我将其配置为原始大小。

height_0 = canvas.winfo_height()
width_0 = canvas.winfo_width()
canvas.config(width= max_width, height= max_height)
root.update()
canvas.postscript(file='filename.ps',colormode='color')
canvas.config(width= width_0, height= height_0)
root.update()