2011-10-31 2 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() 
관련 문제