[Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?
Solaris 10, Python 2.4.4 I have very little experience with issuing print commands using a Unix environment. Despite this, I wish to design a Tkinter window with a "Print" button, which, when clicked, would create a copy of the contents of the window as a .pdf file. GhostScript is available on my systems. I know that the Canvas container has the ability to print itself to a postscript file, but I do not want to use Canvas as my container object. So I am hopeful this is as simple as discovering the proper command to associate with a button, which will generate a color postscript file of the containing window. Once I can get that file, I know how to get a .pdf out of it. The main software that we use on these systems has this print command associated with its color printer *button*: lp -c -d ricoh -o resolution=1200 -o profile=photo -o dithering=photo -o colorsettings=superfine -o black=k -o papper=letter -o itray=tray1 BTW, "papper" is what is actually in this software product's color printer settings. But can this be correct? Anyway, it does print to *paper*! I suspect that some variant of this command associated with a print button would print *something*, but how do I get it to print the specific window containing the to-be-designed print button? Also, I need it to print a postscript file, not print to paper. I suspect this is another configuration setting that I need to research. As always, many thanks in advance! -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?
On Wed, Apr 15, 2015 at 07:55:28AM -0500, boB Stepp wrote: > Solaris 10, Python 2.4.4 > > I have very little experience with issuing print commands using a Unix > environment. Despite this, I wish to design a Tkinter window with a > "Print" button, which, when clicked, would create a copy of the > contents of the window as a .pdf file. GhostScript is available on my > systems. > > I know that the Canvas container has the ability to print itself to a > postscript file, but I do not want to use Canvas as my container > object. You will excuse me, I hope, but I'm afraid that comes across as rather foolish: "I want to hammer this nail into this piece of wood. I have a hammer, but I don't want to use a hammer. I would prefer to use a saw, or perhaps a paint brush. How can I do this?" Why don't you want to use a Canvas? That sounds like it will solve your problem. Use a Canvas as the container, give it a button, and have the button send a message to the Canvas saying "Print yourself to PDF file!" If you explain why you don't wish to use a Canvas, perhaps we can suggest a solution that doesn't involve trying to hammer nails with paint brushes. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?
On Wed, Apr 15, 2015 at 8:29 AM, Steven D'Aprano wrote: > On Wed, Apr 15, 2015 at 07:55:28AM -0500, boB Stepp wrote: >> Solaris 10, Python 2.4.4 >> >> I have very little experience with issuing print commands using a Unix >> environment. Despite this, I wish to design a Tkinter window with a >> "Print" button, which, when clicked, would create a copy of the >> contents of the window as a .pdf file. GhostScript is available on my >> systems. >> >> I know that the Canvas container has the ability to print itself to a >> postscript file, but I do not want to use Canvas as my container >> object. > > You will excuse me, I hope, but I'm afraid that comes across as rather > foolish: > > "I want to hammer this nail into this piece of wood. I have a hammer, > but I don't want to use a hammer. I would prefer to use a saw, or > perhaps a paint brush. How can I do this?" > > > Why don't you want to use a Canvas? That sounds like it will solve your > problem. Use a Canvas as the container, give it a button, and have the > button send a message to the Canvas saying "Print yourself to PDF > file!" > > If you explain why you don't wish to use a Canvas, perhaps we can > suggest a solution that doesn't involve trying to hammer nails with > paint brushes. Perhaps I am being foolish! But I do have my reasons, which, in this case, is I wanted to take advantage of the pack and grid geometry managers. These two tools seem to make the positioning of the widgets much easier. Unless I am missing something, in a Canvas container I will have to assign pixel coordinates for everything, which sounds like a lot more work! In any event, I *would* like to know how to solve the original problem, creating a print button that will print the contents of its overall container object. -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?
On Apr 15, 2015 9:38 AM, "boB Stepp" wrote: > Perhaps I am being foolish! But I do have my reasons, which, in this > case, is I wanted to take advantage of the pack and grid geometry > managers. These two tools seem to make the positioning of the widgets > much easier. Unless I am missing something, in a Canvas container I > will have to assign pixel coordinates for everything, which sounds > like a lot more work! Couldn't you just map a single Frame widget on the canvas, and use the Frame as your "outer" container? -- Zach On a phone ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?
On Wed, Apr 15, 2015 at 8:50 AM, Zachary Ware wrote: > > On Apr 15, 2015 9:38 AM, "boB Stepp" wrote: >> Perhaps I am being foolish! But I do have my reasons, which, in this >> case, is I wanted to take advantage of the pack and grid geometry >> managers. These two tools seem to make the positioning of the widgets >> much easier. Unless I am missing something, in a Canvas container I >> will have to assign pixel coordinates for everything, which sounds >> like a lot more work! > > Couldn't you just map a single Frame widget on the canvas, and use the Frame > as your "outer" container? You are SMART, I am NOT! That sounds like a great idea!! Is this the intent of the Tkinter design to provide print functionality? To always start with a Canvas container? -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?
boB Stepp wrote: > Solaris 10, Python 2.4.4 > > I have very little experience with issuing print commands using a Unix > environment. Despite this, I wish to design a Tkinter window with a > "Print" button, which, when clicked, would create a copy of the > contents of the window as a .pdf file. GhostScript is available on my > systems. > > I know that the Canvas container has the ability to print itself to a > postscript file, but I do not want to use Canvas as my container > object. So I am hopeful this is as simple as discovering the proper > command to associate with a button, which will generate a color > postscript file of the containing window. Once I can get that file, I > know how to get a .pdf out of it. > > The main software that we use on these systems has this print command > associated with its color printer *button*: > > lp -c -d ricoh -o resolution=1200 -o profile=photo -o dithering=photo > -o colorsettings=superfine -o black=k -o papper=letter -o itray=tray1 > > BTW, "papper" is what is actually in this software product's color > printer settings. But can this be correct? Anyway, it does print to > *paper*! > > I suspect that some variant of this command associated with a print > button would print *something*, but how do I get it to print the > specific window containing the to-be-designed print button? Also, I > need it to print a postscript file, not print to paper. I suspect this > is another configuration setting that I need to research. > > As always, many thanks in advance! I'm on linux and surprisingly subprocess.call(["import", "-window", window_title, postscript_file]) worked. I admit it's a roundabout way... Here's the complete experiment; I ran it with Python 2.7, but that shouldn't make a difference. import Tkinter as tk import subprocess window_title = "gewizzede" postscript_file = "tmp_snapshot.ps" def print_canvas(): #canvas.postscript(file="tmp.ps") subprocess.call(["import", "-window", window_title, postscript_file]) root = tk.Tk() root.title(window_title) canvas = tk.Canvas(root, width=200, height=200) canvas.pack() button = tk.Button(root, text="Print", command=print_canvas) button.pack() demo = tk.Button(root, text = "demo widget button") canvas.create_window(50, 50, window=demo) canvas.create_rectangle(25, 25, 75, 150, fill="red") root.mainloop() You might guess from the above that I tried Zachary's suggestion first, but that printed only the red rectangle, not the demo button. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?
On Apr 15, 2015 9:59 AM, "boB Stepp" wrote: > > On Wed, Apr 15, 2015 at 8:50 AM, Zachary Ware > wrote: > > > > On Apr 15, 2015 9:38 AM, "boB Stepp" wrote: > >> Perhaps I am being foolish! But I do have my reasons, which, in this > >> case, is I wanted to take advantage of the pack and grid geometry > >> managers. These two tools seem to make the positioning of the widgets > >> much easier. Unless I am missing something, in a Canvas container I > >> will have to assign pixel coordinates for everything, which sounds > >> like a lot more work! > > > > Couldn't you just map a single Frame widget on the canvas, and use the Frame > > as your "outer" container? > > You are SMART, I am NOT! That sounds like a great idea!! To almost quote Jacob Kaplan-Moss from his keynote talk at PyCon this year, "Hi, I'm [Zach], and I'm a mediocre programmer" :) > Is this the intent of the Tkinter design to provide print > functionality? To always start with a Canvas container? I honestly have no idea. -- Zach On a phone ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?
On 15/04/15 13:55, boB Stepp wrote: Solaris 10, Python 2.4.4 I have very little experience with issuing print commands using a Unix environment. Despite this, I wish to design a Tkinter window with a "Print" button, which, when clicked, would create a copy of the contents of the window as a .pdf file. GhostScript is available on my systems. Your problem is that Tkinter does not really support the concept of printing to hard copy. Other GUI frameworks (like WxPython) do this much better. So anything you do will be a bit of a kluge. There are some valid reasons for this: printing is extremely OS dependant. And Tk as a cross OS platform doesn't want to have to deal with all the complexity(*). The usual solution is to render the output in some print friendly file format (html, groff, SVG etc) and then send that to the appropriate print command. However if you really want a screen image you might be better invoking one of the many screen capture programs and then printing its output? (*) The main culprits are - Windows which uses a "Device Context" to render its UI. The device can be either a screen or a printer (or notionally anything else) so in theory you just swap the DC and send the GUI to the new device. In practice its not that easy. - MacOSX uses a Cocoa framework which, in turn, uses OpenGL primitives to render the UI and like Windows can theoretically render the UI on non screen devices, It can also render it as PDF natively which is how it normally prints screen images. - *nix usually relies on rendering the output *content* on a new device or in a new format. Printing screenshots in generic Unix is a bit of an afterthought IMHO...Possibly a consequence of X having been designed before the days of affordable colour printers - the same reason Steve Jobs didn't bother with colour on the early Macs - if you couldn't print it, there was no point, he thought... Printing UI images and WYSIWYG type output is one of the few triggers that drive me to WxPython. It handles it much more easily than Tk. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?
On Wed, Apr 15, 2015 at 10:39 AM, Peter Otten <__pete...@web.de> wrote: > boB Stepp wrote: > >> Solaris 10, Python 2.4.4 [...] > > I'm on linux and surprisingly > > subprocess.call(["import", "-window", window_title, postscript_file]) > > worked. I admit it's a roundabout way... I did not find the "import" command in my Unix reference, but found it in the man pages on my Solaris workstation. It looks like this should work without having to use the Canvas container, which was what I was originally asking about. > Here's the complete experiment; I ran it with Python 2.7, but that shouldn't > make a difference. Yes, it ran fine on Python 2.4.4. > import Tkinter as tk Question: I have been using "from Tkinter import *" as suggested in "Programming Python" by Lutz. He remarks that unlike other situations, this is generally safe with Tkinter. Is there a benefit to doing the import as you have? > import subprocess > > window_title = "gewizzede" > postscript_file = "tmp_snapshot.ps" > > def print_canvas(): > #canvas.postscript(file="tmp.ps") > subprocess.call(["import", "-window", window_title, postscript_file]) At first I thought you had a typo here, but now I understand that "# canvas.postscript(file="tmp.ps")" only gave you the window framework. When I made my attempt to employ Zach's suggestion by embedding my existing frames within a Canvas, I only got an empty white rectangle. After searching online for that, I found references that Canvas' postscript method only works for objects drawn on its canvas, NOT embedded windows, frames, etc. This had me scratching my head again. But then your post came in. I am very grateful for your solution! > root = tk.Tk() > root.title(window_title) > > canvas = tk.Canvas(root, width=200, height=200) > canvas.pack() > > button = tk.Button(root, text="Print", command=print_canvas) > button.pack() > > demo = tk.Button(root, text = "demo widget button") > canvas.create_window(50, 50, window=demo) > canvas.create_rectangle(25, 25, 75, 150, fill="red") > root.mainloop() > > You might guess from the above that I tried Zachary's suggestion first, but > that printed only the red rectangle, not the demo button. -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?
On Wed, Apr 15, 2015 at 11:13 AM, Alan Gauld wrote: [...] > Your problem is that Tkinter does not really support the concept > of printing to hard copy. Other GUI frameworks (like WxPython) > do this much better. So anything you do will be a bit of a kluge. > There are some valid reasons for this: printing is extremely OS dependant. > And Tk as a cross OS platform doesn't want to have > to deal with all the complexity(*). This is sounding like developing a platform-independent program involving Tkinter and requiring printing is no easy task. Fortunately my efforts need only run successfully on the Solaris 10 OS. [...] > Printing UI images and WYSIWYG type output is one of the few triggers that > drive me to WxPython. It handles it much more easily than Tk. I will have to keep wxPython in mind. It does not help me at work, but for non-work projects it might be very useful. However, I should work on mastering Tkinter first, as I am sure that all of the principles I learn here will be generically useful for any GUI programming. Thanks, Alan! -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?
On 15/04/15 18:00, boB Stepp wrote: import Tkinter as tk Question: I have been using "from Tkinter import *" as suggested in "Programming Python" by Lutz. He remarks that unlike other situations, this is generally safe with Tkinter. Is there a benefit to doing the import as you have? Biggest benefit is if you ever move to Python 3 you cansimply change that line to import tkinter as tk and it carries on working (or at least until you hit some of the other renamed modules!) But also if you ever get to use Tix, because it is a superset of Tkinter so you can just change to import tix as tk and it keeps working but you now have access to all the Tix widgets too. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor