动态显示不同内容(在 Tcl/Tk 中)的好方法是什么?

发布于 2024-09-16 22:39:22 字数 413 浏览 3 评论 0原文

我有一组单选按钮(例如,选择 1 和 2),需要根据用户的选择显示一些小部件。例如,如果他们选择 1,我会向他们展示一个带有多个单选按钮的标签框;然而,如果他们选择 2,我会向他们展示一个带有一些按钮labelframe。无论哪种情况,生成的内容都将显示在窗口的同一区域中。

像这样在多个小部件显示之间切换的好方法是什么?

这个答案让我认为我应该使用 panedwindowframes,但我不太明白如何在不同内容之间切换。

I have a set of radiobuttons (say, with choices 1 and 2) and need to show some widget based off of the user's choice. For example, if they chose 1, I would show them a labelframe with several radiobuttons; whereas, if they chose 2, I would show them a labelframe with some buttons. In either case, the resulting content would be shown in the same area on the window.

What is a good way to switch between multiple widget displays like this?

This answer has me thinking that I should use a panedwindow and frames, but I don't quite see how I would switch between different content.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕烟庭风 2024-09-23 22:39:22

使用 panedwindow 时,您应该能够通过删除框架并添加新框架来在内容窗格之间切换一个来代替它。有关 的说明,请参阅本手册页忘记 t​​tk::panedwindow 的add/insert 命令。

示例代码:

package require Ttk

# Create a panedwindow
ttk::frame .f
ttk::panedwindow .f.pane -orient vertical

# Create three panes
ttk::frame .f.pane.one -height 50 -width 50
ttk::label .f.pane.one.l -text "Number one"
pack .f.pane.one.l

ttk::frame .f.pane.two -height 50 -width 50
ttk::label .f.pane.two.l -text "Number two"
pack .f.pane.two.l

ttk::frame .f.pane.three -height 50 -width 50
ttk::label .f.pane.three.l -text "Number three"
pack .f.pane.three.l

# Add frames one and two to the panedwindow
.f.pane add .f.pane.one
.f.pane add .f.pane.two

pack .f.pane -expand 1 -fill both
pack .f -expand 1 -fill both

# Replace pane one with pane three
.f.pane insert 1 .f.pane.three
.f.pane forget 2 

您可以根据自己的需要调整此代码。只需创建您可能需要的所有视图,然后根据需要将它们换入和换出即可。

When using a panedwindow, you should be able to switch between content panes by deleting a frame and adding a new one to replace it. See this manual page for a description of the forget and add/insert commands for ttk::panedwindow.

Sample code:

package require Ttk

# Create a panedwindow
ttk::frame .f
ttk::panedwindow .f.pane -orient vertical

# Create three panes
ttk::frame .f.pane.one -height 50 -width 50
ttk::label .f.pane.one.l -text "Number one"
pack .f.pane.one.l

ttk::frame .f.pane.two -height 50 -width 50
ttk::label .f.pane.two.l -text "Number two"
pack .f.pane.two.l

ttk::frame .f.pane.three -height 50 -width 50
ttk::label .f.pane.three.l -text "Number three"
pack .f.pane.three.l

# Add frames one and two to the panedwindow
.f.pane add .f.pane.one
.f.pane add .f.pane.two

pack .f.pane -expand 1 -fill both
pack .f -expand 1 -fill both

# Replace pane one with pane three
.f.pane insert 1 .f.pane.three
.f.pane forget 2 

You can adapt this code for your own needs. Simply create all the views that you might need, and then swap them in and out as needed.

那伤。 2024-09-23 22:39:22

一种非常简单的方法是对每组数据使用一个帧。使用grid将它们全部放置在同一行和同一列中。然后您需要做的就是将框架及其子项提升到堆叠顺序的顶部。

另一种技术的起点相同,但不是使用 raise,而是在当前显示的帧上执行 grid remove,然后在要显示的帧上执行 grid。通过grid removegrid会记住所有设置,这样您下次希望它显示时就不必再次指定所有选项。

A really simple way is to use one frame for each set of data. Use grid to place them all in the same row and column. Then all you need to do is raise the frame and it's children to the top of the stacking order.

Another technique starts out the same but instead of using raise, do grid remove on whichever frame is currently being shown, and then grid on the one to be shown. With grid remove, grid remembers all of the settings so that you don't have to specify all of the options again the next time you want it to appear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文