在获取的方法中:AttributeError <classname>实例没有属性<name>

时间:2016-05-15 09:53:28

标签: python python-2.7 oop tkinter

我的主窗口上有一个按钮,当鼠标进入时,想要在标签中显示信息。当鼠标离开按钮时,标签应变为空。它应该像状态栏一样工作,但分为右侧和左侧。

我还试图通过labelname.configure根据Bryan Oakley的帖子更改标签,但它对我没用,所以我仍然使用StringVar版本:

# -*- coding: utf-8 -*-
import Tkinter as Tk, os


class C_MasterGui():
    def __init__(self):
        self.root = Tk.Tk()
        self.root.title("Main window")
        self.root.geometry("900x600+500+300")
        #C_Menubar(self.root)
        C_Buttonbar(self.root)
        C_Workframe(self.root)
        statusbar = C_Statusbar(self.root)
        statusbar.create_statusbar(self.root)

    def open_gui(self):
        self.root.mainloop()

    def close_gui(self):
        pass


class C_Buttonbar():
    def __init__(self,p_parent):
        ICONPATH = os.getcwd() + "/graphics/"
        ICONEXIT = Tk.PhotoImage(file=ICONPATH + "application-exit-5.png")

        self.updatestatus = C_Statusbar(p_parent)                             # Connect to statusbar


        frIconToolbar = Tk.Frame(p_parent, height=30, relief="flat", bd=2)
        frIconToolbar.pack(fill="x")

        butExit = Tk.Button(frIconToolbar, image=ICONEXIT, width=30, relief="groove")
        butExit.bind("<Enter>", self.iconExit_in)
        butExit.bind("<Leave>", self.icon_out)
        butExit.pack(side="left", ipadx=4, ipady=2, padx=4)

    def iconExit_in(self, arg):
        self.updatestatus.set_statusbar("your name","Programm beenden")

    def icon_out(self, arg):
        self.updatestatus.set_statusbar("your name","")


class C_Workframe():
    def __init__(self,p_parent):
        self.workframe = Tk.Frame(p_parent)
        self.workframe.pack(fill="both", expand=1)


class C_Statusbar():
    def __init__(self,p_parent):
        self.parent = p_parent

    def create_statusbar(self,p_parent):
        # Initialize Texts for left and right statusbar
        self.player_data = Tk.StringVar(p_parent)
        self.action_text = Tk.StringVar(p_parent)
        self.set_statusbar("current user: none","")
        statusbar_left = Tk.Label(p_parent, textvariable=self.player_data, relief="groove", bd=2, font="Arial, 10",
                                   anchor="w")
        statusbar_left.pack(side="left", expand="yes",fill="x")
        statusbar_right = Tk.Label(p_parent, textvariable=self.action_text, relief="groove", bd=2, font="Arial, 10",
                                    anchor="e")
        statusbar_right.pack(side="right", expand="yes", fill="x")

    def set_statusbar(self,p_playerdata, p_actiontext):
        print "Statusbar left:", p_playerdata, "Statusbar right:", p_actiontext
        self.player_data.set(p_playerdata)
        self.action_text.set(p_actiontext)


#
#
if __name__ == '__main__':
    myInstance = C_MasterGui()
    myInstance.open_gui()

当鼠标进入或离开按钮时,方法set_statusbar会抛出此错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
    return self.func(*args)
  File "/home/gonzo/Spaces/Dokumente_remote/Python_Programmierprojekte/GolfTracker/test_statusbar.py", line 49, in iconExit_in
    self.updatestatus.set_statusbar("your name","Programm beenden")
  File "/home/gonzo/Spaces/Dokumente_remote/Python_Programmierprojekte/GolfTracker/test_statusbar.py", line 87, in set_statusbar
    self.player_data.set(p_playerdata)
AttributeError: C_Statusbar instance has no attribute 'player_data'

任何人都可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:0)

您需要先创建状态栏:

class C_Statusbar():
    def __init__(self,p_parent):
        self.parent = p_parent
        self.create_statusbar(p_parent)

并在此处删除statusbar.create_statusbar(self.root)

class C_MasterGui():
    def __init__(self):
        self.root = Tk.Tk()
        self.root.title("Main window")
        self.root.geometry("900x600+500+300")
        #C_Menubar(self.root)
        C_Buttonbar(self.root)
        C_Workframe(self.root)
        statusbar = C_Statusbar(self.root)
        # called in `C_Statusbar.__init__)` already
        #statusbar.create_statusbar(self.root)