我看看生活百科网正式上线啦!

python用什么软件编程做界面(高中信息技术python编程分享)

生活百科 wokk 1年前 (2023-04-09) 193次浏览 0个评论

1 说明:

=====

1.1 TraitsUI是一套建立在Traits库基础上的用户界面库。

1.2 系统将会使用TraitsUI自动生成一个界面,以供用户交互式地修改对象的trait属性。

1.3 以traits为基础、以Model-View-Controller为设计思想的TraitUI库就是实现这一理想的最佳伴侣。

1.4 TraitsUI——轻松制作用户界面:在开发科学计算程序时,我们希望快速实现一个够用的界面,让用户能够交互式的处理数据,而又不希望在界面制作上花费过多的精力。

1.5 Traits库:为python的属性增加了类型定义的功能,除此之外他还提供了5个特殊的功能:

初始化:每个traits属性都有自己的默认值。

验证:是traits属性有明确的类型定义,只有满足定义的值时才能给它赋值。

代理:traits属性值可以代理给其他对象实例的属性。

监听:是为了当traits属性发生变化时,可以运行事先指定的函数。

可视化:是拥有traits属性的对象,可以方便的生成可以编辑traits属性的界面。

0b1f5b06-61ab-4170-9406-397273d51b40noop.image_

2 准备:

=====

2.1 官网:

https://github.com/enthought/traitsui
https://docs.enthought.com/traitsui/

2.2 安装:

pip install traits

3 Helloworld:

==========

3.1 代码:

from traits.api import HasTraits
from traitsui.api import View, VGroup,Label
#定义helloworld类
class Helloworld(HasTraits):
    traits_view = View(
        #VGroup=HGroup
        VGroup(
            Label(label='Helloworld=你好世界-1'),
            Label('Helloworld=你好世界-2'),  #等同与上面
            #只能放在下面,但是显示却在上面
            #label='Helloworld=你好世界-3',
            ),
        #窗口大小,标题名,按钮设置;
        width=1000,
        height=1000,
        title='Helloworld',
        buttons=['OK'],  #Ok按钮
        resizable=True, #窗口大小可调节设置
    )

#实例化
demo = Helloworld()

if __name__ == '__main__':
    #可视化demo:那么直接调用其configure_traits方法,系统将会使用TraitsUI自动生成一个界面
    demo.configure_traits()

3.2 图:

40f7064e6d4d49108846299b28087d02noop.image_

4 代码:

from traits.api import HasTraits,Delegate,Instance,Int,Str
class Parent(HasTraits):
    #初始化
    last_name = Str("张")   #初始化
class Child(HasTraits):
    age = Int
    #验证
    father = Instance(Parent)   #定义了father属性是Parent的实例,而此时father的默认属性是None
    #代理
    last_name = Delegate('father')  #通过Delagate为child对象创建了代理属性last_name,代理功能将使得c.last_name和c.father.last_name始终保持相同的值
    #监听
    def _age_changed(self,old,new):
        print("Age change from %s to %s"%(old,new))
p = Parent()  #实例化对象
c = Child()
#正确
c.father = p
c.last_name
#可视化
c.configure_traits()

图:

bd40e2c8690b48dd9ffc59163f14d178noop.image_

5 visible_when:

===========

5.1 代码:

from traits.api import HasTraits, Str, Range, Bool, Enum
from traitsui.api import Item, Group, View, Label

class Person(HasTraits):
    first_name = Str()
    last_name = Str()
    age = Range(0, 120)
    legal_guardian = Str()
    school = Str()
    grade = Range(1, 12)
    marital_status = Enum('single', 'married', 'divorced', 'widowed')
    registered_voter = Bool(False)
    military_service = Bool(False)

    gen_group = Group(
        Item(name='first_name'),
        Item(name='last_name'),
        Item(name='age'),
        label='General Info',
        show_border=True
    )

    child_group = Group(
        Item(name='legal_guardian'),
        Item(name='school'),
        Item(name='grade'),
        label='Additional Info for minors',
        show_border=True,
        visible_when='age < 18',
    )

    adult_group = Group(
        Item(name='marital_status'),
        Item(name='registered_voter'),
        Item(name='military_service'),
        label='Additional Info for adults',
        show_border=True,
        visible_when='age >= 18',
    )

    view = View(
        Group(
            gen_group,
            '10',
            Label("Using 'visible_when':"),
            '10',
            child_group,
            adult_group
        ),
        title='Personal Information',
        resizable=True,
        buttons=['OK']
    )

demo = Person(
    first_name="Samuel",
    last_name="Johnson",
    age=16
)


if __name__ == '__main__':
    demo.configure_traits()

5.2 图:

fa70d9e33b1f4b1cadab4ce51367167bnoop.image_

6 login:

======

6.1 超级简单的登录框,代码:

from traits.api import HasTraits, Str, Int

class ModelManager(HasTraits):
    model_name = Str  #字符串
    category = Str    #字符串
    model_file = Str   #字符串
    model_number = Int  #数值

if __name__ == "__main__":
    model = ModelManager()
    model.configure_traits()

6.2 图:

3e533ca79d214eee860b2660bcd6518enoop.image_

7 treeeditor:

=========

7.1 代码:

from traits.api import HasTraits, Str, Regex, List, Instance
from traitsui.api import Item, View, TreeEditor, TreeNode

class Employee(HasTraits):
    #name = Str('<unknown>')
    name = Str()
    title = Str()
    phone = Regex(regex=r'\d\d\d-\d\d\d\d')
    def default_title(self):
        self.title = 'Senior Engineer'

class Department(HasTraits):
    name = Str('<unknown>')
    employees = List(Employee)

class Company(HasTraits):
    name = Str('<unknown>')
    departments = List(Department)
    employees = List(Employee)

no_view = View()

tree_editor = TreeEditor(
    nodes=[
        TreeNode(
            node_for=[Company],
            auto_open=True,
            children='',
            label='name', 
            view=View(['name'])
        ),
        TreeNode(
            node_for=[Company],
            auto_open=True,
            children='departments',
            label='=Departments',  # constant label
            view=no_view,
            add=[Department],
         ),
        TreeNode(
            node_for=[Company],
            auto_open=True,
            children='employees',
            label='=Employees',   # constant label
            view=no_view,
            add=[Employee]
        ),
        TreeNode(
            node_for=[Department],
            auto_open=True,
            children='employees',
            label='name',   # label with Department name
            view=View(['name']),
            add=[Employee]
        ),
        TreeNode(
            node_for=[Employee],
            auto_open=True,
            label='name',   # label with Employee name
            view=View(['name', 'title', 'phone'])
        )
    ]
)


class Partner(HasTraits):
    name = Str('<unknown>')
    company = Instance(Company)

    traits_view = View(
        Item(name='company', editor=tree_editor, show_label=False),
        title='Company Structure',
        buttons=['OK'],
        resizable=True,
        style='custom',
        width=.3,
        height=500
    )

jason = Employee(name='Jason', title='Senior Engineer', phone='536-1057')
mike = Employee(name='Mike', title='Senior Engineer', phone='536-1057')
dave = Employee(name='Dave', title='Senior Developer', phone='536-1057')
martin = Employee(name='Martin', title='Senior Engineer', phone='536-1057')
duncan = Employee(name='Duncan', title='Consultant', phone='526-1057')

demo = Partner(
    name='Enthought, Inc.',
    company=Company(
        name='Enthought',
        employees=[dave, martin, duncan, jason, mike],
        departments=[
            Department(
                name='Business',
                employees=[jason, mike]
            ),
            Department(
                name='Scientific',
                employees=[dave, martin, duncan]
            )
        ]
    )
)

if __name__ == '__main__':
    demo.configure_traits()

7.2 图:

ed44decebbc94c729bfd7036aa6160dcnoop.image_

8 codeeditor:

==========

8.1 代码:

from traits.api import HasTraits, Code
from traitsui.api import Item, Group, View

class CodeEditorDemo(HasTraits):
    code_sample = Code('import sys\n\nsys.print("hello world!")')

    code_group = Group(
        Item('code_sample', style='simple', label='Simple'),
        Item('_'),
        Item('code_sample', style='custom', label='Custom'),
        Item('_'),
        Item('code_sample', style='text', label='Text'),
        Item('_'),
        Item('code_sample', style='readonly', label='ReadOnly')
    )

    traits_view = View(
        code_group,
        #窗口标题名。大小,按钮ok
        title='CodeEditor',
        width=600,
        height=600,
        buttons=['OK']
    )

demo = CodeEditorDemo()

if __name__ == "__main__":
    demo.configure_traits()

8.2 图:

77dcad23b8cf4e86b434c67f09edfdb9noop.image_

===超级简单,简化GUI的布局===

自己整理并分享出来。

喜欢的人,请点赞、关注、评论、转发和收藏。

喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址