系统:windows 7语言版本:anaconda3-4.3.0.1-windows-x86_64编辑器:jetbrains pycharm community edition 2016.3.2
本系列将探讨
traits
traitsui
Str
Button
第一部分:示例
本示例提供了一个界面,用户可以在输入框中输入内容,并通过点击按钮显示输入的内容。以下是运行结果的截图:

第二部分:代码
代码语言:Python 代码运行次数:0
import win32api
import win32con
from traits.api import HasTraits, Str, Button, on_trait_change
from traitsui.api import View, Item, Group, HGroup, VGroup
class Ui(HasTraits):
input = Str()
button = Button("点我")
def _button_fired(self):
input_str = self.input
tips = "输入内容为:\n\n{0}".format(input_str)
win32api.MessageBox(0, tips, "提示", win32con.MB_OK)
g1 = [Item("input", style="simple", label="输入内容", tooltip="写入字符串", emphasized=True),
"_",
Item("button", style="simple", show_label=False)]
view = View(Group(*g1, label="控件Str-Button", show_border=True),
resizable=True, title="ui")
ui = Ui()
ui.configure_traits()代码截图:

第三部分:代码解读
整体代码分为三个部分:
def
View
首先,导入所需的模块。然后定义界面类的类,并运行界面。
如何定义按钮点击事件的响应函数?定义一个函数,函数名称必须是固定的格式:
def _控件名称_fired(self):
如何获取
Str
self.控件变量名
self.input
录入提示使用
tooltip
emphasized
关于
Item

以上就是【traits-traitsui-基础】-02-控件Str/Button的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号