user2175850
user2175850

Reputation: 193

Changing Fonts, Size , Background Color of Traits in TraitsUI

I am using TraitsUI to make a GUI. I want to be able to edit more about how the GUI actually looks. E.g. I want to be able to change the font of certain Str objects, change the background color of some boxes, make some boxes much larger with larger font sizes (bold/italic etc.).

Is this easy to do? I have been playing around with the toy example below. But all attempts I have made have not worked. Do I need to edit the View or Item Objects to do this? Or do I need to create custom Editors?

A simple example highlighting how to do these things would be appreciated if anyone know of one.

Thanks,

Tim

class House(HasTraits):
    address = Str
    bedrooms = Int
    pool = Bool
    price = Int

    traits_view =View(
        Group(Item('address', style="readonly"), Item('bedrooms'), Item('pool'), Item('price'),show_border=True)
        )

hs = House()
hs.configure_traits()

Upvotes: 1

Views: 1149

Answers (1)

user2175850
user2175850

Reputation: 193

I have found a way of solving this problem. I would agree the traitsui package doesn't really suite having formatting change under certain conditions. I was able to do it by using a custom Handler to access the controller and then using the PyQt object to change the formatting.

In the below clicking the Pool check box will change the background colour. You can also access the other children widgets to do more specific tasks.

I would be interested if anyone knows a better/ more traits based way.

from traits.api import *
from traitsui.api import *
import PyQt4



class HouseHandler(Handler):

    def object_pool_changed(self,info):
        if info.object.pool:
            print info.ui.control
            print info.ui.control.children()
            qtObject = info.ui.control
            palette = qtObject.palette()
            qtObject.setAutoFillBackground(True)
            palette.setColor(qtObject.backgroundRole(), PyQt4.QtCore.Qt.red)
            qtObject.setPalette(palette)
            #or with style sheets
            #info.ui.control.setStyleSheet('background-color: red')
        else:
            info.ui.control.setStyleSheet('background-color: None')


class House(HasTraits):
    address = Str
    bedrooms = Int
    pool = Bool
    price = Int
    traits_view =View(
            Group(Item('address', style="readonly"), Item('bedrooms'), Item('pool'), Item('price'),show_border=True),
            handler = HouseHandler()
        )

hs = House()
hs.configure_traits()

Upvotes: 3

Related Questions