简体   繁体   English

尝试从 python 中的另一个 class 访问 StringVar 时出现 AttributeError

[英]AttributeError when trying to access StringVar from another class in python

So I've been working on a Personal Training App in python I have a Start Up window, referenced in the code as StartUp.所以我一直在 python 中开发个人培训应用程序,我有一个启动 window,在代码中引用为 StartUp。 Once the user enters the name of the client that they want to see records for, I'm trying to get the name variable from the StartUp class to the Application class.一旦用户输入他们想要查看记录的客户端的名称,我就会尝试将名称变量从 StartUp class 获取到应用程序 class。 However, when I run it, it gives me the following error但是,当我运行它时,它给了我以下错误

Traceback (most recent call last):
File "/usr/lib/python3.6/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/home/ninja65/PtApp/main.py", line 32, in <lambda>
old = tk.Button(mainframe, text='Open', command=lambda:[parent.destroy(), Workspace()],bg='sienna3')
File "/home/ninja65/PtApp/main.py", line 16, in Workspace
App = Application(root)
File "/home/ninja65/PtApp/main.py", line 55, in __init__
self.target = StartUp.target
AttributeError: type object 'StartUp' has no attribute 'target'

the Code is below代码如下

# !usr/bin/python3
import tkinter as tk
import json

def CreateNew():
  temp = tk.Tk()
  temp.title('Enter New Client')
  New = MakeNew(temp)
  temp.eval('tk::PlaceWindow . center')
  temp.mainloop()

def Workspace():
  root = tk.Tk()
  root.config(bg='grey9')
  root.title("Personal Trainer")
  App = Application(root)
  root.eval('tk::PlaceWindow . center')
  root.mainloop()

class StartUp():
  def __init__(self, parent):    
    mainframe = tk.Frame(parent, bg='firebrick4')
    mainframe.grid(column=0, row=0, sticky=tk.N)
    # Adding widgets to the frame
    opening = tk.Label(mainframe, text='Welcome, what do you want to do?', font='tahoma', bg='firebrick4', fg='khaki3')
    guide = tk.Label(mainframe, text="Type the Client's name with no gaps", font='tahoma', bg='firebrick4', fg='khaki3')
    guide.grid(columnspan=10, row=1)
    opening.grid(columnspan=10, row=0, pady=5, padx=5)
    self.target = tk.StringVar()
    open_entry = tk.Entry(mainframe, textvariable=self.target)
    open_entry.grid(columnspan=7, row=2, sticky=tk.W+tk.E, padx=5)
    old = tk.Button(mainframe, text='Open', command=lambda:[parent.destroy(), Workspace()], bg='sienna3')
    old.grid(column=7, columnspan=4, row=2, sticky=tk.W+tk.E, pady=5, padx=5)
    new = tk.Button(mainframe, text='New Client', command=lambda:[parent.destroy(), CreateNew()], bg='sienna3', font='tahoma')
    new.grid(columnspan=10, row=3, sticky=tk.W+tk.E, pady=5, padx=5)


class MakeNew:

def __init__(self, loader):
    menubutton = tk.Menubutton(loader, text = "File")
    menubutton.grid()
    menubutton.menu = tk.Menu(menubutton)
    menubutton["menu"]=menubutton.menu
    menubutton.menu.add_checkbutton(label = "New file")
    menubutton.menu.add_checkbutton(label = "Save")
    menubutton.menu.add_checkbutton(label = "Save as")
    menubutton.pack()

def method(self):
    pass

class Application(StartUp):
  def __init__(self, master):
    self.target = StartUp.target
    menubutton = tk.Menubutton(master, text = "File",font='gothic', bg='#86C232')
    menubutton.grid(column=0, row=0, sticky=tk.W+tk.E)
    menubutton.menu = tk.Menu(menubutton, bg='#86C232')
    menubutton["menu"]=menubutton.menu
    menubutton.menu.add_checkbutton(label = "New file")
    menubutton.menu.add_checkbutton(label = "Save")
    menubutton.menu.add_checkbutton(label = "Save as")
    # Frame of vital info
    v_frame = tk.Frame(master, bg='gray16')
    v_frame.grid(column=0, row=1, sticky=tk.W+tk.E)
    # Frame of Additional info
    a_frame = tk.Frame(v_frame)
    a_frame.grid(column=5, row=0, sticky=tk.E+tk.W+tk.N)
    # Frame of Exercise routine
    e_frame = tk.Frame(master, bg='grey18')
    e_frame.grid(column=0, row=2, sticky=tk.S)
    # First Row of Vital Info
    cl_name = tk.Label(v_frame, text=self.target, bg='gray15', fg='#61892F',font='tahoma').grid(column=0, row=0, sticky=tk.W+tk.E)
    cl_age = tk.Label(v_frame, text='Age', bg='gray15', fg='#61892F',font='tahoma').grid(column=1, row=0, sticky=tk.W+tk.E)
    cl_height = tk.Label(v_frame, text='Height', bg='gray15', fg='#61892F',font='tahoma').grid(column=2, row=0, sticky=tk.W+tk.E)
    cl_weight = tk.Label(v_frame, text='Weight', bg='gray15', fg='#61892F',font='tahoma').grid(column=3, row=0, sticky=tk.W+tk.E)
    cl_sex = tk.Label(v_frame, text='Gender', bg='gray15', fg='#61892F',font='tahoma').grid(column=4, row=0, sticky=tk.W+tk.E)
    # Second Row of Vital Info
    cl_bfp = tk.Label(v_frame, text='Fat %', bg='gray15', fg='#61892F',font='tahoma').grid(column=0, row=1, sticky=tk.W+tk.E)
    cl_lbm = tk.Label(v_frame, text='Lean Mass', bg='gray15', fg='#61892F',font='tahoma').grid(column=1, row=1, sticky=tk.W+tk.E)
    cl_gw = tk.Label(v_frame, text='Goal Weight', bg='gray15', fg='#61892F',font='tahoma').grid(column=2, row=1, sticky=tk.W+tk.E)
    cl_bmi = tk.Label(v_frame, text='BMI', bg='gray15', fg='#61892F',font='tahoma').grid(column=3, row=1, sticky=tk.W+tk.E)
    # Last Row
    cl_goal = tk.Label(v_frame, text='Goals that the client put go here', bg='gray15', fg='#61892F',font='tahoma').grid(columnspan=5, row=2, sticky=tk.W+tk.E)
    # Additional Column
    atest = tk.Label(a_frame, text='Test', bg='gray22').grid(column=0, row=0)
    # Exercise Routine Column
    mday_label = tk.Label(e_frame, text='Monday', bg='gray18', fg='#61892F', font='gothic').grid(column=0, row=0)
    tday_label = tk.Label(e_frame, text='Tuesday', bg='gray18', fg='#61892F', font='gothic').grid(column=1, row=0)
    wday_label = tk.Label(e_frame, text='Wednesday', bg='gray18', fg='#61892F', font='gothic').grid(column=2, row=0)
    thday_label = tk.Label(e_frame, text='Thursday', bg='gray18', fg='#61892F', font='gothic').grid(column=3, row=0)
    fday_label = tk.Label(e_frame, text='Friday', bg='gray18', fg='#61892F', font='gothic').grid(column=4, row=0)
    saday_label = tk.Label(e_frame, text='Saturday', bg='gray18', fg='#61892F', font='gothic').grid(column=5, row=0)
    sday_label = tk.Label(e_frame, text='Sunday', bg='gray18', fg='#61892F', font='gothic').grid(column=6, row=0)
    mday = tk.Listbox(e_frame, bg='gray22').grid(column=0, row=1)
    tday = tk.Listbox(e_frame, bg='gray22').grid(column=1, row=1)
    wday = tk.Listbox(e_frame, bg='gray22').grid(column=2, row=1)
    thday = tk.Listbox(e_frame, bg='gray22').grid(column=3, row=1)
    fday = tk.Listbox(e_frame, bg='gray22').grid(column=4, row=1)
    stday = tk.Listbox(e_frame, bg='gray22').grid(column=5, row=1)
    sday = tk.Listbox(e_frame, bg='gray22').grid(column=6, row=1)

def method(self):
    pass

if __name__ == '__main__':
  sub = tk.Tk()
  sub.config(bg='firebrick4')
  sub.title('PT App Login')
  Window = StartUp(sub)
  sub.eval('tk::PlaceWindow . center')
  sub.mainloop()

I see at least two problems.我看到至少两个问题。 For one, you are trying to reference Startup.target , which is a reference to a class variable.一方面,您尝试引用Startup.target ,这是对class变量的引用。 You haven't defined a class variable named target .您尚未定义名为target的 class 变量。 You have instead defined an instance variable named target.相反,您定义了一个名为 target 的实例变量。

Also, your Application inherits from Startup but it does not call the __init__ method of Startup so the object is incorrectly initialized.此外,您的ApplicationStartup继承,但它不调用Startup__init__方法,因此 object 初始化不正确。 Since Application inherits from Startup , it should be initialized like this:由于Application继承自Startup ,它应该像这样初始化:

class Application(StartUp):
  def __init__(self, master):
    super().__init__(master)
    ...

Once you do that, there's no reason to do self.target = Startup.target since self.target already exists.一旦你这样做了,就没有理由做self.target = Startup.target因为self.target已经存在。

There are other bugs in the code, but this explains the error your question is asking about and how to fix that specific error.代码中还有其他错误,但这解释了您的问题所询问的错误以及如何修复该特定错误。

暂无
暂无

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

相关问题 尝试从wxpython中的另一个类访问列表时遇到AttributeError - Running into AttributeError when trying to access a list from another class in wxpython 尝试将输入与Python 3中的类的内容进行匹配时发生AttributeError - AttributeError when trying to match input with content from a class in Python 3 尝试使用从第二个类中的该类调用另一个函数的类函数时,出现“ AttributeError” - Getting an “AttributeError” when trying to use a class function that calls another function from that class inside a second class Python:unittest,AttributeError 试图从测试类的 __init__ 访问 self 属性 - Python: unittest, AttributeError trying to access self property from __init__ of tested class AttributeError:类型对象“ class”没有属性“ stringVar” - AttributeError: type object 'class' has no attribute 'stringVar' 尝试访问确实存在的 class 方法时出现 AttributeError - AttributeError when trying to access class methods that do exist AttributeError:在尝试从Tkinter按钮调用时,StringVar实例没有属性“endswith” - AttributeError: StringVar instance has no attribute 'endswith' while trying to call from a Tkinter button tkinter:如何从不同的 class 访问 StringVar 和对象? - tkinter: How to access a StringVar and objects from a different class? Python参数返回“ <class 'tkinter.StringVar'> 打印时 - Python argument returning “<class 'tkinter.StringVar'>” when printing 尝试访问python函数时出现AttributeError - AttributeError in trying to access a python function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM