2014-08-31 91 views
1

我有利用Tkinter的GUI如何将图像组件添加到tkinter GUI?

一个相对简单的Python脚本

代码看起来像

from Tkinter import * 

master = Tk() 

def f1(): 
    print "function f1 does stuff, nice.." 


title = Label(text="Tweet Grabber 1.1") 
title.pack(fill=X) 

b = Button(master, text="OK", command=f1) 
b.pack(fill=X) 

mainloop() 

使用Tkinter的是能够增加图像的组成部分?

最终在那里我可以去像img = image(src="path/imagename.filetype")

然后img.pack()

是否有可能使用图像与Tkinter的一个按钮?

回答

1

这是可能的,你可以使用PIL(Python图像库),但没有必要下面的代码应该工作:

from Tkinter import * 

master = Tk() 

def f1(): 
    print "function f1 does stuff, nice.." 

def imageclick(): 
    print 'you clicked the button, nice...' 

image = PhotoImage(file = 'directory or name of file.gif (if image is in same folder as .py file)') 

img_button = Button(image = image, command = imageclick) 
img_button.pack() 

title = Label(text="Tweet Grabber 1.1") 
title.pack(fill=X) 

b = Button(master, text="OK", command=f1) 
b.pack(fill=X) 

mainloop() 

如果这不起作用打电话给我,我的天堂没有在python 2.7上测试过,但它应该工作:),希望对你有所帮助。

+0

@ L.Clarkson编辑是一个很好的补充,但它应该是一个真正的评论;) – W1ll1amvl 2014-12-23 19:57:22