'级'对象没有属性' tk'

时间:2017-01-07 15:05:48

标签: python tkinter

我已经看过其他线程的答案,这些答案与我的问题非常相似,但我似乎无法让它发挥作用。

class Application(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        Container = tk.Frame(self)
        Container.pack(side="top", fill="both", expand=True)
        Container.grid_rowconfigure(0, weight=1)
        Container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (Login, Admin):
            PageName = F.__name__
            frame = F(parent=Container, controller=self)
            self.frames[PageName] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(Login)

    def show_frame(self, PageName):
        frame = self.franes[PageName]
        frame.tkraise()


class Login(tk.Frame):
    def __init__(self, parent, controller):
        super().__init__(self, parent) (...)

我正在使用此代码跟随另一个线程回答,以便在帧之间切换。

错误:

AttributeError: 'Login' object has no attribute 'tk'

Traceback (most recent call last):
  File "G:/Jake/Google Drive/StockControl/Framework/src/Login.py", line 70, in <module>
    App = Application()
  File "G:/Jake/Google Drive/StockControl/Framework/src/Login.py", line 15, in __init__
    frame = F(parent=Container, controller=self)
  File "G:/Jake/Google Drive/StockControl/Framework/src/Login.py", line 28, in __init__
    super().__init__(self, parent)
  File "C:\Utilities\Python\lib\tkinter\__init__.py", line 2738, in __init__
    Widget.__init__(self, master, 'frame', cnf, {}, extra)
  File "C:\Utilities\Python\lib\tkinter\__init__.py", line 2286, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Utilities\Python\lib\tkinter\__init__.py", line 2256, in _setup
    self.tk = master.tk
AttributeError: 'Login' object has no attribute 'tk'

任何帮助将不胜感激,谢谢! (Python 3.6)

1 个答案:

答案 0 :(得分:0)

在Python 3中使用superself参数自动作为第一个参数加前缀;你的定义应该是这样的:

class Login(tk.Frame):
    def __init__(self, parent, controller):
        super().__init__(parent)
相关问题