Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter Canvas Postscript未捕获屏幕框架外的窗口元素_Python_Canvas_Tkinter_Postscript - Fatal编程技术网

Python Tkinter Canvas Postscript未捕获屏幕框架外的窗口元素

Python Tkinter Canvas Postscript未捕获屏幕框架外的窗口元素,python,canvas,tkinter,postscript,Python,Canvas,Tkinter,Postscript,正如标题所说,当尝试使用Postscript保存画布时,它可以很好地处理所有非窗口元素(矩形、椭圆形等),并且当我按下按钮时,它可以完美地捕获当前屏幕上的窗口元素。但当时屏幕外没有任何窗口元素 这个问题似乎太武断了,我想知道是否有解决办法,希望有人已经想出了一些办法 下面是一些示例代码,我在其中简化以说明确切的问题: #!/usr/bin/python3 # # This file is intended as a simplified example for Stack Overflow. #

正如标题所说,当尝试使用Postscript保存画布时,它可以很好地处理所有非窗口元素(矩形、椭圆形等),并且当我按下按钮时,它可以完美地捕获当前屏幕上的窗口元素。但当时屏幕外没有任何窗口元素

这个问题似乎太武断了,我想知道是否有解决办法,希望有人已经想出了一些办法

下面是一些示例代码,我在其中简化以说明确切的问题:

#!/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()
#/usr/bin/python3
#
#此文件旨在作为堆栈溢出的简化示例。
#原始程序要大得多,是一种用于分支对话的编写工具,非常类似于Twine。
从tkinter导入Tk、画布、框架、文本、标签
Canv类(画布):
定义初始化(自身,父级):
“”“简单画布类。”“”
画布。\uuuuu初始化(自,父)
self.parent=parent
self.config(background=“white”,宽度=960,高度=640)
self.num=1
self.pack()
self.bindings()
def绑定(自):
“”“所有按钮绑定。”“”
self.bind(“,self.add_窗口)
self.bind(“,self.mark)
self.bind(“,self.drag)
self.bind(“,self.take\ps)
def添加窗口(自我,e):
“”“在这里,我将标签添加为画布窗口。
并包括一个椭圆形标记其位置。
"""
text=“Textwindow{}”。格式(self.num)
self.num+=1
窗口=文本窗口(自身,文本)
pos=(self.canvasx(e.x),self.canvasy(e.y))
自创建窗口(位置,窗口=窗口)
bbox=(位置[0]-50,位置[1]-50,位置[0]+50,位置[1]+50)
自行创建椭圆(长方体,宽度=3,轮廓=“绿色”)
def标记(自身,e):
“”“简单的标记拖动方法。”“”
自我扫描标记(e.x,e.y)
def阻力(自,e):
“”“这将使用鼠标中键拖动画布以四处移动。”“”
自扫描绘图(e.x,e.y,5)
def取数(自身,e):
“这里我要一个画布的.ps文件。
请记住,画布实际上是无限的,因此我需要设置.ps文件的大小
指向画布上每个当前元素的边界框。
"""
x1,y1,x2,y2=自身.bbox(“全部”)
self.postscript(file=“outfile.ps”,colormode=“color”,x=x1,y=y1,width=x2,height=y2)
打印(“写入文件outfile.ps…”)
类文本窗口(框架):
定义初始化(自身、父项、文本):
“”“非常简单的标签类。
可能有点过火了,我本来打算在这门课上多做些,
但事实证明,这个例子是不必要的。
"""
帧。\uuuu初始化(自,父)
self.pack()
self.label=label(self,text=text)
self.label.pack()

如果uuu name uuu==“uuuu main”:#我也遇到了这个问题。我能够临时配置画布以匹配输出图像的大小。然后,在创建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()

如果你仍然迫切需要解决这个问题,我可以明天给你100个代表的奖金。那真的很酷。这里有一个链接,指向最有可能是相关的C代码:这里有一些
NULL
语句可能是问题所在,但我不会说C:-/至于
tk_state\u hidden
,这不是问题所在,因为它很容易测试。这似乎没有帮助,对不起,伙计。您可能需要重新表述问题并重新发布。你可以把标题改成一个更简单的,我从没想过我会这么说,更容易点击的标题。同时改进图片:不要链接后面的图片,而是将它们包含在帖子中。“我拥有什么”和“我想要什么”的图片往往有助于这个问题的流行。你可以把问题的主体缩短一点,使之简短。去掉更多的代码,将其缩小到尽可能小的范围。补充:对于StackOverflow问题,您不需要代码是“完美的”,您希望它尽可能简短和干净,同时仍然能够产生您的问题。例如,去掉所有的文档字符串、注释、
如果uuuu name\uuuuuuu=='\uuuuu main\uuuuuuu':
、额外的打印和功能等等。