Fri, Nov 22, 11:32 AM CST

Renderosity Forums / Poser Python Scripting



Welcome to the Poser Python Scripting Forum

Forum Moderators: Staff

Poser Python Scripting F.A.Q (Last Updated: 2024 Sep 18 2:50 am)

We now have a ProPack Section in the Poser FreeStuff.
Check out the new Poser Python Wish List thread. If you have an idea for a script, jot it down and maybe someone can write it. If you're looking to write a script, check out this thread for useful suggestions.

Also, check out the official Python site for interpreters, sample code, applications, cool links and debuggers. This is THE central site for Python.

You can now attach text files to your posts to pass around scripts. Just attach the script as a txt file like you would a jpg or gif. Since the forum will use a random name for the file in the link, you should give instructions on what the file name should be and where to install it. Its a good idea to usually put that info right in the script file as well.

Checkout the Renderosity MarketPlace - Your source for digital art content!



Subject: Setting button colors in a dialog?


maclean ( ) posted Sat, 18 April 2009 at 12:51 PM · edited Sun, 17 November 2024 at 5:17 AM

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 · edited Sat, 18 April 2009 at 1:02 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)

My python page
My ShareCG freebies


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.




Privacy Notice

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.