为什么我会收到此错误? AttributeError:类型对象“App”没有属性“tk”

问题描述 投票:0回答:0

此代码在运行时返回错误:“AttributeError: type object 'App' has no attribute 'tk'”。

import tkinter as tk
from tkinter import ttk


def entryField(parent):
    frame = ttk.Frame(master = parent)
    
    return frame

class App(tk.Tk):
    def __init__(self):
    
        #main setup
        super().__init__()
                
        #widgets
        entryField(App).pack()

        self.mainloop()


App()

仅当在 App 类内调用函数 entryField 时才会抛出错误,当在 App 类外部调用它时,它可以正常工作。我不明白为什么,因为我的印象是将其定义为类 App(tk.Tk) 应该赋予它 tk 属性。难道不能以这种方式组合基于类和函数的小部件吗?我在其他地方找到的所有示例都不涉及从基于类的小部件中调用基于函数的小部件。

python class user-interface tkinter attributeerror
© www.soinside.com 2019 - 2024. All rights reserved.