正如标题所述,当尝试使用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按钮时,我的屏幕在右圆上。
至于替代方案,我需要画布是可拖动的,我需要将其扩展,可能在两个方向上都有很大的距离。而且我不能将文本直接放在画布上,因为这会占用太多空间。它旨在具有文本字段,而不仅仅是画布上窗口中的标签(在此示例中成为过多的代码),并且我需要在窗口中而不是直接在屏幕上显示文本的原因是文本可能很容易占用更多空间我需要画布来显示文本字段之间的关系,而文本窗口则包含要编辑的文本(不一定是完整显示)。就像我说的那样,我正在为游戏制作分支对话工具,就像Twine一样。
我也遇到了这个问题。我能够临时配置画布以匹配输出图像的大小。创建完后记文件后,然后将其配置回原始大小。