简体   繁体   English

在TraitsUI中更改特征的字体,大小,背景颜色

[英]Changing Fonts, Size , Background Color of Traits in TraitsUI

I am using TraitsUI to make a GUI. 我正在使用TraitsUI制作GUI。 I want to be able to edit more about how the GUI actually looks. 我希望能够编辑有关GUI实际外观的更多信息。 Eg 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.). 例如,我希望能够更改某些Str对象的字体,更改某些框的背景颜色,使用更大的字体大小(粗体/斜体等)使某些框大得多。

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

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. 我同意traitsui套件在某些情况下并不会真正改变格式。 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. 通过使用自定义处理程序访问控制器,然后使用PyQt对象更改格式,我能够做到这一点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM