如何动态填充 TK 组合框的值?
我是 tcl/tk 编程新手。这是组合框上的一个小代码片段。如何动态添加和删除组合框中的值?
set ff [ frame f]
set label [Label $ff.label -text "Name:" ]
set name [ComboBox $ff.name \
-editable yes \
-textvariable name]
set addButton [Button $ff.addButton -text "+" -width 1 -command {addNameToComboBox}]
set removeButton [Button $ff.removeButton -text "-" -width 1 -command removeNameFromComboBox}]
grid $ff.addButton -row 0 -column 2 -sticky w
grid $ff.removeButton -row 0 -column 3 -sticky sw -padx 5
proc addNameToComboBox {name} {
}
proc removeNameFromComboBox {name} {
}
干杯!
I am new to tcl/tk programming. Here is a small code snippet on combo box. How can I dynamically add and remove values from the combo box?
set ff [ frame f]
set label [Label $ff.label -text "Name:" ]
set name [ComboBox $ff.name \
-editable yes \
-textvariable name]
set addButton [Button $ff.addButton -text "+" -width 1 -command {addNameToComboBox}]
set removeButton [Button $ff.removeButton -text "-" -width 1 -command removeNameFromComboBox}]
grid $ff.addButton -row 0 -column 2 -sticky w
grid $ff.removeButton -row 0 -column 3 -sticky sw -padx 5
proc addNameToComboBox {name} {
}
proc removeNameFromComboBox {name} {
}
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的示例代码有一些错误(*),并且并不完全清楚您想要做什么。您是否想要将组合框的当前值添加到下拉列表中,或者您想要添加的值来自其他地方?
这是一个将组合框的当前值添加到列表中的解决方案。它使用内置版本的组合框、标签和按钮小部件。无论您使用什么组合框小部件,其工作原理都可能类似,尽管可能不完全一样。
(*) Button、Label 和 ComboBox 不是标准小部件 - 您的意思是“button”、“label”和“ttk::combobox”还是您正在使用一些自定义小部件?另外,您忘记使用网格来管理组合框和标签,并且您的过程需要参数,但您没有传递任何参数)。
该解决方案适用于 tcl/tk 8.5 和内置 ttk::combobox 小部件:
Your example code has a few bugs (*), and it's not entirely clear what you want to do. Are you wanting to add the current value of the combobox to the dropdown list, or is the value you want to add coming from somewhere else?
Here's a solution that adds the current value of the combobox to the list. It uses the built-in versions of the combobox, label and button widgets. Whatever combobox widget you are using probably works similarly, though maybe not exactly.
(*) Button, Label and ComboBox aren't standard widgets -- did you mean "button", "label" and "ttk::combobox" or are you using some custom widgets?. Also, you forgot to use grid to manage the combobox and label, and your procs are expecting arguments but you aren't passing any in).
This solution works with tcl/tk 8.5 and the built-in ttk::combobox widget: