正如标题所说,当尝试使用 Postscript 保存 Canvas 时,它适用于所有非窗口元素(矩形、椭圆形等),并且在我按下时捕获当前在屏幕上的窗口元素时效果很好按钮。但当时屏幕外没有任何窗口元素。
这个问题看起来太武断了,我想知道是否有解决方案,希望有人已经想出了办法。
这是一些示例代码,我在其中进行了简化以呈现确切的问题:
#!/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 按钮时,我的屏幕在正确的圆圈上方。
至于替代方案,我需要 Canvas 可以拖动,我需要它可以在两个方向上扩展,可能距离很远。而且我不能将文本直接放在 Canvas 上,因为它会占用太多空间。它旨在具有文本字段,而不仅仅是 Canvas 窗口中的标签(对于此示例来说代码太多),我需要窗口中而不是直接在屏幕上的文本的原因是文本可能很容易占用更多空间。我需要 Canvas 来显示文本字段之间的关系,以及文本窗口来包含要编辑的文本,不一定要完整显示。正如它所说,我正在为游戏制作一个分支对话工具,很像 Twine。
最佳答案
我也遇到了这个问题。我能够临时配置 Canvas 以匹配输出图像的大小。然后我在创建 postscript 文件后将其配置回原始大小。
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()
关于python - Tkinter Canvas Postscript 不捕获屏幕框架外的 Window 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40866367/