Python 在TKinter窗口中创建图形?
我正在编写一个脚本,它将运行数据并创建一个图形。这很容易做到。不幸的是,我使用的图形模块只创建pdf格式的图形。不过,我希望在交互式窗口中显示图形Python 在TKinter窗口中创建图形?,python,tkinter,pyx,Python,Tkinter,Pyx,我正在编写一个脚本,它将运行数据并创建一个图形。这很容易做到。不幸的是,我使用的图形模块只创建pdf格式的图形。不过,我希望在交互式窗口中显示图形 他们是否可以将用创建的图形添加到TKinter窗口中,或将pdf加载到帧或其他内容中?您需要将PyX输出转换为位图,以将其包含在TKinter应用程序中。虽然没有方便的方法直接将PyX输出为PIL图像,但可以使用pipeGS方法准备位图并使用PIL加载它。下面是一个非常简单的例子: import tempfile, os from pyx impo
他们是否可以将用创建的图形添加到TKinter窗口中,或将pdf加载到帧或其他内容中?您需要将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()