简体   繁体   中英

Disable TAB TKINTER python

I am new to python and I am trying to disable TAB2 from widget notebook tkinter, through the support_test.py file I have the command of the disable_tab2 button, which should have the command to disable the option, but I get the error below:

Exception in Tkinter callback Traceback (most recent call last):   File
"C:\Users\Ryzen\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.p>y", line 1705, in __call__
    return self.func(*args)   File "c:\teste\teste_support.py", line 20, in desativa_tab2
    w.TNotebook1_t0.configure(state='disabaled')   File "C:\Users\Ryzen\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1485, in configure
    return self._configure('configure', cnf, kw)   File "C:\Users\Ryzen\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1476, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-state"

file teste.py

#  -*- coding: utf-8 -*-

import sys

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True


import teste_support

def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = tk.Tk()
    top = Toplevel1 (root)
    teste_support.init(root, top)
    root.mainloop()

w = None
def create_Toplevel1(root, *args, **kwargs):
    '''Starting point when module is imported by another program.'''
    global w, w_win, rt
    rt = root
    w = tk.Toplevel (root)
    top = Toplevel1 (w)
    teste_support.init(w, top, *args, **kwargs)
    return (w, top)

def destroy_Toplevel1():
    global w
    w.destroy()
    w = None

class Toplevel1:
    def __init__(self, top=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#ececec' # Closest X11 color: 'gray92'
        self.style = ttk.Style()
        if sys.platform == "win32":
            self.style.theme_use('winnative')
        self.style.configure('.',background=_bgcolor)
        self.style.configure('.',foreground=_fgcolor)
        self.style.configure('.',font="TkDefaultFont")
        self.style.map('.',background=
            [('selected', _compcolor), ('active',_ana2color)])

        top.geometry("600x450+633+190")
        top.minsize(120, 1)
        top.maxsize(1924, 1061)
        top.resizable(1, 1)
        top.title("New Toplevel")
        top.configure(background="#d9d9d9")

        self.Button1 = tk.Button(top)
        self.Button1.place(relx=0.417, rely=0.044, height=24, width=47)
        self.Button1.configure(activebackground="#ececec")
        self.Button1.configure(activeforeground="#000000")
        self.Button1.configure(background="#d9d9d9")
        self.Button1.configure(command=teste_support.desativa_tab2)
        self.Button1.configure(disabledforeground="#a3a3a3")
        self.Button1.configure(foreground="#000000")
        self.Button1.configure(highlightbackground="#d9d9d9")
        self.Button1.configure(highlightcolor="black")
        self.Button1.configure(pady="0")
        self.Button1.configure(text='''Button''')

        self.style.configure('TNotebook.Tab', background=_bgcolor)
        self.style.configure('TNotebook.Tab', foreground=_fgcolor)
        self.style.map('TNotebook.Tab', background=
            [('selected', _compcolor), ('active',_ana2color)])
        self.TNotebook1 = ttk.Notebook(top)
        self.TNotebook1.place(relx=0.067, rely=0.222, relheight=0.591
                , relwidth=0.69)
        self.TNotebook1.configure(takefocus="")
        self.TNotebook1_t0 = tk.Frame(self.TNotebook1)
        self.TNotebook1.add(self.TNotebook1_t0, padding=3)
        self.TNotebook1.tab(0, text="Page 1",compound="left",underline="-1",)
        self.TNotebook1_t0.configure(background="#d9d9d9")
        self.TNotebook1_t0.configure(highlightbackground="#d9d9d9")
        self.TNotebook1_t0.configure(highlightcolor="black")
        self.TNotebook1_t1 = tk.Frame(self.TNotebook1)
        self.TNotebook1.add(self.TNotebook1_t1, padding=3)
        self.TNotebook1.tab(1, text="Page 2",compound="left",underline="-1",)
        self.TNotebook1_t1.configure(background="#d9d9d9")
        self.TNotebook1_t1.configure(highlightbackground="#d9d9d9")
        self.TNotebook1_t1.configure(highlightcolor="black")

        self.Label1 = tk.Label(self.TNotebook1_t0)
        self.Label1.place(relx=0.195, rely=0.333, height=21, width=104)
        self.Label1.configure(background="#d9d9d9")
        self.Label1.configure(disabledforeground="#a3a3a3")
        self.Label1.configure(foreground="#000000")
        self.Label1.configure(text='''TAB 1''')

        self.Label1 = tk.Label(self.TNotebook1_t1)
        self.Label1.place(relx=0.263, rely=0.258, height=21, width=104)
        self.Label1.configure(activebackground="#f9f9f9")
        self.Label1.configure(activeforeground="black")
        self.Label1.configure(background="#d9d9d9")
        self.Label1.configure(disabledforeground="#a3a3a3")
        self.Label1.configure(foreground="#000000")
        self.Label1.configure(highlightbackground="#d9d9d9")
        self.Label1.configure(highlightcolor="black")
        self.Label1.configure(text='''TAB 2''')

if __name__ == '__main__':
    vp_start_gui()

file teste_suporte.py

#  -*- coding: utf-8 -*-


import sys

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

def desativa_tab2():
    global w
    w.TNotebook1_t0.configure(state='disabaled')
    print('teste_support.desativa_tab2')
    sys.stdout.flush()

def init(top, gui, *args, **kwargs):
    global w, top_level, root
    w = gui
    top_level = top
    root = top

def destroy_window():
    # Function which closes the window.
    global top_level
    top_level.destroy()
    top_level = None

if __name__ == '__main__':
    import teste
    teste.vp_start_gui()

You must use the tab method to configure an individual tab, just like you're doing in some other places in your code. The first argument is a reference to the tab, such as the integer index of the tab. Following that is the state option with the desired value.

For example:

self.TNotebook1.tab(0, state="disabled")

I have made the following changes in the desativa_tab2() function inside of the teste_suporte.py file to disable the tab 2 in the notebook.

I used notebook_name.tabs() that will return a list of tab references in the notebook in order of first tab1, then tab2 and so on..

So, this notebook has two tabs and we want to disable the tab 2 therefore, grab the tab2 reference from the list of tabs at index 1.

tab2 = w.TNotebook1.tabs()[1]

Then select the tab2 from the notebook using notebook_name.tab() and then disable that very tab using the tab2 reference grabbed in the previous step

w.TNotebook1.tab(tab2, state='disabled')

Here is the complete desativa_tab2() function:

def desativa_tab2():
    global w

    tab2_name = w.TNotebook1.tabs()[1]
    w.TNotebook1.tab(tab2_name, state='disabled')

    print('teste_support.desativa_tab2')
    sys.stdout.flush()

Replace this function in your teste_suporte.py file. I tried to explain what changes I made with a bit of theory. Hope you understand!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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