4

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 (i.e. prevent the user from switching tabs, and ghost non-active tabs out to make this fact obvious). 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). I've worked with Tkinter a bit but never Tix until now-tabs seem to require it. 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

For disabling a tab, the only relevant attribute of the Tix tab instance (e.g. nb.hard_disk in the demo code) seems to be configure() but naively doing something Tkinter-like, i.e. nb.hard_disk.configure(state=Tix.DISABLED), results in 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. Grateful for any pointers in the right direction.

Fenikso
  • 9,251
  • 5
  • 44
  • 72
jez
  • 14,867
  • 5
  • 37
  • 64

2 Answers2

11

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". 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. 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()
nbro
  • 15,395
  • 32
  • 113
  • 196
Karim Bahgat
  • 2,781
  • 3
  • 21
  • 27
  • Hmm, thanks... That sooort-of helps, in that it puts me on the track of ttk instead of Tix. Following up, it seems Tix is pretty much obsolete and ttk, which I hadn't heard of, is the way to go. But unfortunately for various annoying reasons I will have to do a lot of work before I can finally break free from having to support Python 2.5—and out of the box, Python 2.5 for Windows comes with Tix rather than ttk. So I'm really looking for a Tix solution. – jez Jan 10 '14 at 23:06
  • I guess I was under the impression that you wanted EITHER a Tkinter solution OR a Tix solution (I misspelled it as Wx in my answer by the way). And maybe ttk isnt really the same as Tkinter, but as I understand it is an addon/extension for Tkinter which you need among other things in order to use the Notebook widget. Well unfortunately I dont have any Tix advice to give, but hope like you say that maybe my example puts you on track to the correct Tix solution. – Karim Bahgat Jan 13 '14 at 14:28
  • I'll flag this as the best solution because in a sense it is, even if it's not the one that solved my problem. I awarded the bounty to Jason Lv because he actually cracked the case for me: the `pageconfigure` approach is the one I can use given my non-ideal constraint of sticking to Tix, and I doubt I would have found it myself. – jez Jan 14 '14 at 23:39
1

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

Jason Lv
  • 101
  • 3
  • That wins the bounty because it put me on track to a solution I can use in my current system, even though I acknowledge that starting again from scratch with ttk (Karim's approach) would be neater. There is no `.pageconfigure` method but after a bit of googling, and landing on https://mail.python.org/pipermail/tkinter-discuss/2009-April/001916.html , I found the working solution `nb.tk.call(nb._w, 'pageconfigure', 'hard_disk', '-state', 'disabled')` – jez Jan 14 '14 at 23:27