Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;对象没有属性“;python中的错误_Python_Python 3.x_Tkinter - Fatal编程技术网

&引用;对象没有属性“;python中的错误

&引用;对象没有属性“;python中的错误,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,下面是我用Python tkinter编写的代码 import tkinter as tk from tkinter import * from tkinter import ttk root = Tk() class UICreation(): def __init__(self): print ("I m in __init__") tabControl = ttk.Notebook(root) tab1 = ttk.Frame(ta

下面是我用Python tkinter编写的代码

import tkinter as tk
from tkinter import *
from tkinter import ttk
root = Tk()

class UICreation():
    def __init__(self):
        print ("I m in __init__")
        tabControl = ttk.Notebook(root)
        tab1 = ttk.Frame(tabControl)
        tab2 = ttk.Frame(tabControl)

    def tabCreation(self):
        print ("I M in Tab Creation")
        self.tabControl.add(self.tab1 , text="select ")
        self.tabControl.add(self.tab2, text="Add ")
        self.tabControl.grid()

    def checkBox(self):
        print ("I M in checkBox")
        CheckBox1 = Checkbutton(self.tab1, text="Check1")
        CheckBox1.grid()

if __name__ == '__main__':
    ui = UICreation()
    ui.tabCreation()
    ui.checkBox()
    root.mainloop()
下面是错误

我在init 回溯(最近一次呼叫最后一次): 我正在创建选项卡 文件“C:/Path/FileName.py”,第26行,在 ui.tabCreation() 文件“C:/Path/FileName.py”,第15行,在tabCreation中 self.tabControl.add(self.tab1,text=“选择”) AttributeError:“UICreation”对象没有属性“tabControl”


请帮我解决这个问题。我做错了什么以及如何解决它

UICreation
在实例化时从不接收root。同时修复
self
,使其成为实例属性

class UICreation():
    def __init__(self, root): #add root here
        print ("I m in __init__")
        self.tabControl = ttk.Notebook(root)
        self.tab1 = ttk.Frame(self.tabControl)
        self.tab2 = ttk.Frame(self.tabControl)

    def tabCreation(self):
        print ("I M in Tab Creation")
        self.tabControl.add(self.tab1 , text="select ")
        self.tabControl.add(self.tab2, text="Add ")
        self.tabControl.grid()

    def checkBox(self):
        print ("I M in checkBox")
        CheckBox1 = Checkbutton(self.tab1, text="Check1")
        CheckBox1.grid()

if __name__ == '__main__':
    ui = UICreation(root) # call with root
    ui.tabCreation()
    ui.checkBox()
    root.mainloop()

您的
\uuuu init\uuuu
方法可能应该是:

def __init__(self):
    print ("I m in __init__")
    self.tabControl = ttk.Notebook(root)
    self.tab1 = ttk.Frame(self.tabControl)
    self.tab2 = ttk.Frame(self.tabControl)

您缺少每个属性的首字母
self.

您应该将
tabControl
选项卡1
选项卡2
设置为
self

def __init__(self):
    print ("I m in __init__")
    self.tabControl = ttk.Notebook(root)
    self.tab1 = ttk.Frame(self.tabControl)
    self.tab2 = ttk.Frame(self.tabControl)

我们必须将
tabcontrol
tab1
tab2
设置为self并与self一起使用

以下是问题的答案:

   import tkinter as tk
    from tkinter import *
    from tkinter import ttk
    root = Tk()

    class UICreation():
        def __init__(self):
            print ("I m in __init__")
            self.tabControl = ttk.Notebook(root)
            self.tab1 = ttk.Frame(self.tabControl) #we must use self.tabControl
            self.tab2 = ttk.Frame(self.tabControl) #we must use self.tabControl

        def tabCreation(self):
            print ("I M in Tab Creation")
            self.tabControl.add(self.tab1 , text="select ")
            self.tabControl.add(self.tab2, text="Add ")
            self.tabControl.grid()

        def checkBox(self):
            print ("I M in checkBox")
            CheckBox1 = Checkbutton(self.tab1, text="Check1")
            CheckBox1.grid()

    if __name__ == '__main__':
        ui = UICreation()
        ui.tabCreation()
        ui.checkBox()
        root.mainloop()

我按照建议做了以下更改,但仍然存在错误
回溯(上次调用):我在ui=UICreation()文件“C:/Path/FileName.py”的第25行,在ui=UICreation()文件“C:/Path/FileName.py”的第10行,在uu init\uuuu self.tab1=ttk.Frame(tabControl)name错误:名称“tabControl”未定义
@sreevathsabr当然,对不起,我想现在已经修复了。但我仍然面临着错误
我正在进行标签创建回溯(最近一次调用):文件“C:/Path/FileName.py”,第26行,在ui.tabCreation()文件“C:/Path/FileName.py”,第15行,在tabCreation self.tabControl.add(self.tab1,text=“select”)中AttributeError:“UICreation”对象没有属性“tabControl”
我已更新了答案。这不是root的问题。你只需要问自己“我在哪里创建
self.tabControl
?”@BryanOakley我找到了问题和解决方案。我在下面补充了答案