简体   繁体   English

禁用 TAB TKINTER python

[英]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:我是 python 新手,我试图通过 support_test.py 文件从小部件笔记本 tkinter 禁用 TAB2 我有 disable_tab2 按钮的命令,它应该有禁用该选项的命令,但我收到以下错误:

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文件 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文件 teste_support.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.您必须使用tab方法来配置单个选项卡,就像您在代码中的其他一些地方所做的一样。 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.接下来是具有所需值的state选项。

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.我在teste_suporte.py文件内的desativa_tab2()函数中进行了以下更改,以disable笔记本中的tab 2

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..我使用notebook_name.tabs()将返回笔记本中的选项卡引用列表,按第一个 tab1 的顺序,然后是 tab2,依此类推。

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.所以,这个笔记本有两个选项卡,我们想要禁用选项卡 2,因此,从索引 1 的选项卡列表中获取 tab2 引用。

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然后使用notebook_name.tab()从笔记本中选择 tab2,然后使用在上一步中获取的 tab2 引用disable该选项卡

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

Here is the complete desativa_tab2() function:这是完整的desativa_tab2()函数:

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.teste_suporte.py文件中替换此函数。 I tried to explain what changes I made with a bit of theory.我试图用一些理论来解释我所做的改变。 Hope you understand!希望你能理解!

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

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