Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:11:20+00:00 2026-05-30T15:11:20+00:00

I have written a pretty convoluted script for creating graphics – via the Tkinter

  • 0

I have written a pretty convoluted script for creating graphics – via the Tkinter module.

It works as I expect, and saves the canvas as a PostScript file.

Only, I can’t render the PS file in anything. At all.

I was pretty confident when I decided to use Tkinter that there would be something I could do with the PS files to get them into some more standard format.

I have seen some tutorials that suggest porting the drawings to PIL, which might work, but will be quite a task to port all the dynamic objects from tk to PIL.

I wondered if anyone had a quicker/dirtier way of getting the pixels off the widget window into an image file.

Or any windows methods of viewing/rasterising the PS file? I can place an example PS file somewhere if there is interest? (the python code is quite involved, and requires 3 MySQL tables to pull the data together)

I am trying to use the screengrab method from here: http://mail.python.org/pipermail/image-sig/2003-May/002292.html

and and struggle to get things in the right order.

tkinter code:

def drawCircles(MasterList,buildlist):
master = Tk()
w = Canvas(master, width=1000, height=1000)
w.config(bg='white')
coordsMain = MasterList[6:]
textMain = MasterList[0:2]
w.pack()
w.create_oval(*coordsMain, width=3, fill = "ivory3")
masterLabel = "Source PUID\n" + str(MasterList[3]) + "\nFiles = " + str(MasterList[4])
w.create_text(*textMain, text=masterLabel, justify = "center", font=("Helvetica", 16))
for i in buildlist:
 coordsSet = i[6:10]
 w.create_oval(*coordsSet, width=3, fill = i[5])
 set_label = i[3] + "\n" + str(i[4]) + "%"
 l=w.create_text(4,4, text=set_label, justify = "center", fill="white", font=("Helvetica", 16))
 a,b,c,d= (w.bbox(l))
 bboxArea =(c-a)*(d-b)   
 a,b,c,d = i[6:10]
 circleArea = (c-a)*(d-b)
 if bboxArea>circleArea:
  textSet = i[10:]
  j=w.create_text(*textSet, text=set_label, justify = "center", font=("Helvetica", 16))
  r=w.create_rectangle(w.bbox(j),fill="white", width=0)
 else:
  textSet = i[:2]
  j=w.create_text(*textSet, text=set_label, justify = "center", font=("Helvetica", 16))
  r=w.create_rectangle(w.bbox(j),fill=i[5], width=0) 
 w.tag_lower(r,j)
 PUID = str(MasterList[3])
 PUID = PUID.replace('/', '-')
 filename = "\images\\" + PUID + ".jpg"
mainloop()

Screen grab code:

x0 =w.winfo_rootx()
y0 =w.winfo_rooty()
x1 =x0 + w.winfo_width()
y1 =y0 + w.winfo_height()
im =ImageGrab.grab((x0, y0, x1, y1))
im.save(filename)

I can make a jpg this way, but can’t seem to get the contents of the widget in the the jgp (the file that gets created declares as a jpg, but has no image payload)

If I place the screen grab code after the mainloop, it has says its destroyed the object, before mainloop, its not built the object yet….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:11:22+00:00Added an answer on May 30, 2026 at 3:11 pm

    It turns out the update() function is required prior to postscript call, as specified by tk canvas manpage.

    pathName postscript ?option value option value …?

    Note: by default Postscript is only generated for information that appears in the canvas’s window on the screen. If the canvas is freshly created it may still have its initial size of 1×1 pixel so nothing will appear in the Postscript. To get around this problem either invoke the “update” command to wait for the canvas window to reach its final size, or else use the -width and -height options to specify the area of the canvas to print.

    I have marked this as the fix because the PS file is the preferred save format, and is of much higher quality than the screengrab method.

    The working code now becomes:

    def drawCircles(MasterList,buildlist):
    master = Tk()
    w = Canvas(master, width=1000, height=1000)
    w.config(bg='white')
    coordsMain = MasterList[6:]
    textMain = MasterList[0:2]
    w.pack()
    w.create_oval(*coordsMain, width=3, fill = "ivory3")
    masterLabel = "Source PUID\n" + str(MasterList[3]) + "\nFiles = " + str(MasterList[4])
    w.create_text(*textMain, text=masterLabel, justify = "center", font=("Helvetica", 16))
    for i in buildlist:
     coordsSet = i[6:10]
     w.create_oval(*coordsSet, width=3, fill = i[5])
     set_label = i[3] + "\n" + str(i[4]) + "%"
     l=w.create_text(4,4, text=set_label, justify = "center", fill="white", font=("Helvetica", 16))
     a,b,c,d= (w.bbox(l))
     bboxArea =(c-a)*(d-b)   
     a,b,c,d = i[6:10]
     circleArea = (c-a)*(d-b)
     if bboxArea>circleArea:
      textSet = i[10:]
      j=w.create_text(*textSet, text=set_label, justify = "center", font=("Helvetica", 16))
      r=w.create_rectangle(w.bbox(j),fill="white", width=0)
     else:
      textSet = i[:2]
      j=w.create_text(*textSet, text=set_label, justify = "center", font=("Helvetica", 16))
      r=w.create_rectangle(w.bbox(j),fill=i[5], width=0) 
     w.tag_lower(r,j)
     PUID = str(MasterList[3])
     PUID = PUID.replace('/', '-')
     filename = "\images\\" + PUID + ".PS"
     w.update()
    mainloop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some code that works pretty well, but I have a strange
I have written a pretty basic upload script that takes the file and uploads
I have written something like this pretty easily in C# ( string GetUrl(new {
I have written what I thought to be a pretty solid Linq statement but
I've written an entire app pretty successfully in Django but I have this nagging
I have written a ruby script which opens up dlink admin page in firefox
i am trying to implement pretty url. Everything i have written in PHP and
I have written a pretty simple code to get the first result for any
I have written a pretty simple Windows service in C# that starts up automatically
I have written a ruby script to screen scrape something using the 'open-uri' and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.