简体   繁体   English

如何在Tkinter / Tix Python GUI中启用和禁用选项卡

[英]how to enable & disable tabs in a Tkinter/Tix Python GUI

I'm trying to make a tabbed GUI in Python and I want to be able to toggle the enabled/disabled state of the tabs (ie prevent the user from switching tabs, and ghost non-active tabs out to make this fact obvious). 我正在尝试在Python中创建一个选项卡式GUI,我希望能够切换选项卡的启用/禁用状态(即阻止用户切换选项卡,并禁止鬼非活动选项卡使这一事实显而易见)。 So far I've been unable to figure out how to do this state toggling. 到目前为止,我一直无法弄清楚如何进行这种状态切换。

I've decided to go with Tkinter and/or Tix because they come built into Python distros on Windows, (guiding my users through installing extra third-party dependencies will be more trouble than it's worth). 我决定使用Tkinter和/或Tix,因为它们内置于Windows上的Python发行版中(指导我的用户安装额外的第三方依赖项将比它的价值更麻烦)。 I've worked with Tkinter a bit but never Tix until now-tabs seem to require it. 我已经与Tkinter一起工作了一段时间,但是直到现在Tabs似乎不需要Tix为止。 So I've built a two-tabbed Tix.NoteBook based on the demo at http://svn.python.org/projects/python/trunk/Demo/tix/samples/NoteBook.py 因此,我基于位于http://svn.python.org/projects/python/trunk/Demo/tix/samples/NoteBook.py上的演示构建了一个两标签的Tix.NoteBook。

For disabling a tab, the only relevant attribute of the Tix tab instance (eg nb.hard_disk in the demo code) seems to be configure() but naively doing something Tkinter-like, ie nb.hard_disk.configure(state=Tix.DISABLED) , results in TclError: unknown option "-state" 对于禁用选项卡,Tix选项卡实例的唯一相关属性(例如,演示代码中的nb.hard_disk )似乎是configure()但是天真地做了类似Tkinter的操作,即nb.hard_disk.configure(state=Tix.DISABLED) ,导致TclError: unknown option "-state"

Searches for "disable Tix notebook tab" yield nothing, and even the more general "disable Tix widget" yields nothing I can understand/use. 搜索“禁用Tix笔记本选项卡”不会产生任何效果,甚至更通用的“禁用Tix小部件”也不会产生任何我能理解/使用的内容。 Grateful for any pointers in the right direction. 感谢任何正确方向的指针。

In general how you disable widgets in Tkinter is by setting the "state" option to Tk.DISABLED or more foolproof just setting it to a string saying "disabled" . 通常,如何在Tkinter中禁用小部件是通过将“ state”选项设置为Tk.DISABLED或更简单的方法,只需将其设置为表示"disabled"的字符串即可。 The following grays out and disables your tab: 以下显示为灰色并禁用您的标签:

notebook.tab(0, state="disabled")

with 0 being the index of the tab you want to disable, and notebook being your notebook object. 其中0是要禁用的选项卡的索引,而Notebook是您的Notebook对象。 Does that answer your question? 这是否回答你的问题?

Below is a simple notebook example to demonstrate: 下面是一个简单的笔记本示例来演示:

import Tkinter
import ttk

window = Tkinter.Tk()
notebook = ttk.Notebook(window)
notebook.pack()
subframe = Tkinter.Frame(window)
subframe.pack()
notebook.add(subframe, text="tab", state="normal")
def buttonaction():
    notebook.tab(0, state="disabled")
button = Tkinter.Button(subframe, command=buttonaction, text="click to disable tab")
button.pack()

if __name__ == "__main__":
    window.mainloop()

This might be what you are looking for: 这可能是您正在寻找的:

nb.pageconfigure('hard_disk', state=Tix.DISABLED)

http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixNoteBook.htm#M27 http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixNoteBook.htm#M27

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

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