Forum Moderators: Staff
Poser Python Scripting F.A.Q (Last Updated: 2024 Sep 18 2:50 am)
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/
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
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.
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.
Sorry - better write: import ImageTk (without "as PILTK")
Pil can be downloaded from here: http://www.pythonware.com/products/pil/index.htm
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)
My python page
My ShareCG freebies
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.
This site uses cookies to deliver the best experience. Our own cookies make user accounts and other features possible. Third-party cookies are used to display relevant ads and to analyze how Renderosity is used. By using our site, you acknowledge that you have read and understood our Terms of Service, including our Cookie Policy and our Privacy Policy.
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.