maclean opened this issue on Apr 18, 2009 · 12 posts
maclean posted Sat, 18 April 2009 at 12:51 PM
Can anyone tell me what method python uses to set the color of a button in dialogs? I'm trying to modify a script which uses lines like this
self.buttonBack = Button(self.master, text="Back", bg="lightgreen",command=self.HandleBack)
Is there any option other than naming the color, (like 'lightgreen')? Can I use RGB codes for colors? If not, what names are acceptable in python? I've found a few by experimenting, but it's tedious work.
TIA
mac
PS I did trying searching for this subject, but I kept getting hundreds of unrelated posts.
adp001 posted Sat, 18 April 2009 at 1:00 PM
self.buttonBack = Button(self.master, fg="#ffffff", bg="#000000" ....
("fg" or "foreground", "bg" or "background"; parameter: same notation as in HTML)
Tkinter docu: http://effbot.org/tkinterbook/
maclean posted Sat, 18 April 2009 at 1:18 PM
Wow! That was fast!
Thanks very much. And thanks for the link. I've been on google for ages hunting down python pages.
cheers
mac
markschum posted Sat, 18 April 2009 at 1:37 PM
www.python.org is a good start point .
maclean posted Sat, 18 April 2009 at 3:52 PM
Hi adp,
I have a question. I was looking through the docs you linked to, and on this page
http://effbot.org/tkinterbook/tkinter-standard-dialogs.htm
it mentions an 'icon (constant)' option.
Does this mean icons can be used on a dialog button in place of text? I couldn't find any more information on this in the docs.
mac
maclean posted Sat, 18 April 2009 at 4:16 PM
I think I found it.
image=
Although I'm not sure what the path would be. Somewhere in the python library? And are .pngs ok as images?
Sorry. I'm fumbling about in the dark with this stuff. LOL.
mac
nruddock posted Sat, 18 April 2009 at 4:31 PM
Quote - I think I found it.
image=
Although I'm not sure what the path would be. Somewhere in the python library? And are .pngs ok as images?
Sorry. I'm fumbling about in the dark with this stuff. LOL.
mac
My tcl/tk is a little rusty, but IIRC, image handling is limited unless PIL is available.
There are two image types available without PIL :-
BitmapImage -> http://effbot.org/tkinterbook/bitmapimage.htm
PhotoImage -> http://effbot.org/tkinterbook/photoimage.htm
Only PhotoImages will be any use to you, and without PIL, you're essentially limited to GIFs.
adp001 posted Sat, 18 April 2009 at 4:59 PM
nruddock is right. Only GIFs or XBM-files (a format mostly used on unixoid OS's).
The way out is indeed PIL. It's easy to use if you have found out to handle it.
The following code is untested, but will give an idea:
import Image as PIL
import ImageTk as PILTK
class ImageButton(Button) :
def __init__(self, master, **kw) :
img=kw.pop("image",None)
if img:
self.TKimage=ImageTk.PhotoImage(PIL.open(img))
kw["image"]=self.TKimage
else :
self.TKimage=None
Button.__init__(self, master, **kw)
The class ImageButton can now be used like a standard button. If you give an imagename, the image is opened via PIL (so nearly any format is supported) and a Tk-compatible image is buffered in class ImageButton (this is needed to avoid that the image is removed via Pythons garbage-collection). The filename is than replaced with a pointer to the image, so the original init-call is done properly.
adp001 posted Sat, 18 April 2009 at 5:03 PM
Sorry - better write: import ImageTk (without "as PILTK")
Pil can be downloaded from here: http://www.pythonware.com/products/pil/index.htm
maclean posted Sat, 18 April 2009 at 6:20 PM
Thanks a lot, guys. I'll check out these links and try it out.
cheers
mac
ockham posted Sun, 19 April 2009 at 1:35 PM
Here's a working example of a GIF on a button.
Two fussy points:
You have to initialize the PhotoImage variable before using it on the button, and
the PhotoImage variable MUST be a global variable.
from Tkinter import *
global imHelp
...........
imHelp=PhotoImage(file="c:poser 7runtimepythonposerScriptsHelp.gif")
self.HelpButton=Button(self.master,image=imHelp,command=self.HandleHelp)
self.HelpButton.grid(row=0,column=1,sticky=E)
adp001 posted Sun, 19 April 2009 at 2:36 PM
Quote - Here's a working example of a GIF on a button.
Two fussy points:
You have to initialize the PhotoImage variable before using it on the button, and
the PhotoImage variable MUST be a global variable.
PhotoImage variable must be protected from being killed by Pythons garbage collection, as I wrote previously. That does not mean variable PhotoImage must be global! It can also be a class-variable. It can also be carried in Button itself.
img=Photoimg( ... )
button=Button( master, image=img, ... )
button.img=img
will work perfectly. The Advantage is, you don't have to take care about your images. They are there as long as the "container" lives (Button in this example) and will be "garbage-collected" as soon as Button isn't used anymore.