2014-08-31 66 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