3

As the title says, when attempting to save the Canvas using Postscript, it works fine with all non-window elements (rects, ovals etc..) and it works perfectly in capturing window elements that are CURRENTLY on screen when I push the button. But none of the window elements outside of the screen at the time.

This issue seems so arbitrary I gotta wonder if there even is a solution, hopefully someone out there has figured something out.

Here is some example code, where I simplify to present the exact issue:

#!/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.

As you can see from the image, all the green circles on the right have the window label intact. Well ALL the green circles are supposed to have them, and in the program they work fine, the are just not showing in the postscript. And yes, my screen was over the right circles when I clicked the take_ps button.

As for alternatives, I need the canvas to be draggable, I need it to expand, potentially vast distances in both direction. And I cannot put text directly on the canvas, as it would take too much space. It is intended to have text fields, not just a label in the windows on the canvas (became too much code for this example), and the reason I need text in a window, and not directly on the screen, is the text might easily take more space then it should. I need the canvas to show the RELATION between the text fields, and the text windows to contain the text for editing, not necessarily full display. As it says, I'm making a branching dialogue tool for a game, much like Twine.

Mahi
22.4k23 gold badges91 silver badges129 bronze badges
asked Nov 29, 2016 at 12:50
4
  • 1
    If you still desperately need to figure this out, I can throw you a 100 rep bounty tomorrow. Commented Nov 30, 2016 at 15:12
  • That would be really cool. Here is a link to what is most likely the relevant C code: tcl github There are a few NULL statements there that might be the issue, but I don't speak C. :-/ As for the tk_state_hidden, that is not the issue, as its easily testable. Commented Dec 2, 2016 at 11:51
  • Doesn't seem to have helped, sorry mate. You might want to rephrase the question and repost it. You could improve the title to a simpler and, I never thought I'd say this, clickbaitier one. Also improve the images: instead of images behind a link, include them in the post itself. And "what I have" vs. "what I want" images often help gain popularity for the question. You could probably shorten the question's body itself a little, make it short and pithy. Get rid of some more code, narrow it down to as little as possible. Commented Dec 9, 2016 at 13:25
  • To add: You don't need the code to be "perfect" for a StackOverflow question, you want it to be as short and clean as possible while still being able to produce your issue. For example, get rid of all the docstrings, comments, if __name__ == '__main__':, extra prints and features, everything. Commented Dec 9, 2016 at 13:29

1 Answer 1

0

I ran into this issue also. I was able to configure the canvas temporarily to match the size of the output image. Then I configured it back to the original size after I was done creating the postscript file.

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()
answered Mar 20, 2020 at 0:46
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.