2011-10-31 82 views
2

我正在編寫將通過數據運行並創建圖形的腳本。這很容易做到。不幸的是,我正在使用的圖形模塊只能以pdf格式創建圖形。不過,我希望將圖形顯示在交互式窗口中。在TKinter窗口中創建圖形?

他們有什麼辦法將用PyX創建的圖形添加到TKinter窗口或將PDF加載到框架或其他東西?

回答

3

您需要將PyX輸出轉換爲位圖以將其包含在您的Tkinter應用程序中。雖然沒有方便的方法直接將PyX輸出作爲PIL圖像,但可以使用pipeGS方法準備位圖並使用PIL加載它。這裏是一個相當簡單的例子:

import tempfile, os 

from pyx import * 
import Tkinter 
import Image, ImageTk 

# first we create some pyx graphics 
c = canvas.canvas() 
c.text(0, 0, "Hello, world!") 
c.stroke(path.line(0, 0, 2, 0)) 

# now we use pipeGS (ghostscript) to create a bitmap graphics 
fd, fname = tempfile.mkstemp() 
f = os.fdopen(fd, "wb") 
f.close() 
c.pipeGS(fname, device="pngalpha", resolution=100) 
# and load with PIL 
i = Image.open(fname) 
i.load() 
# now we can already remove the temporary file 
os.unlink(fname) 

# finally we can use this image in Tkinter 
root = Tkinter.Tk() 
root.geometry('%dx%d' % (i.size[0],i.size[1])) 
tkpi = ImageTk.PhotoImage(i) 
label_image = Tkinter.Label(root, image=tkpi) 
label_image.place(x=0,y=0,width=i.size[0],height=i.size[1]) 
root.mainloop()