1

I have a relatively simple python script utilizing a tkinter GUI

the code looks something like

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()

Using tkinter is it possible to add an image component?

ultimately where I can go something like img = image(src="path/imagename.filetype")

then img.pack()

also

Is it possible to use an image as a button with tkinter?

1 Answer 1

1

This is possible, you can use PIL (python imaging library), but it isn't necessary the below code should work:

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()

if this doesn't work get back to me, I haven't tested this on python 2.7 but it should work :), hope that helps you.

Sign up to request clarification or add additional context in comments.

1 Comment

@L.Clarkson Edit was an alright addition, however it should have been a comment really ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.