我有一个现成的Python 3程序,我没有使用任何面向对象的技术(这是我的第一个Python程序)快速而又脏地编写的。现在是时候清理它了,我在课堂上让tkinter工作时遇到了问题。为什么我不能使用tkinter网格方法放置这个类?
这里是一个简化的例子。它只是试图将ttk.Entry小部件放入类,这是从ttk.Frame继承:
import tkinter as tk
from tkinter import ttk
class MyClass(ttk.Frame):
def __init__(self, parent):
self.frame = ttk.Frame(parent)
# Entry widget
self.entValue = ttk.Entry(self.frame)
self.entValue.grid(column=0, row=0)
# tkinter init
root = tk.Tk()
# Make the class instance and place it
test = MyClass(root)
test.grid(column=0, row=0)
它给了我下面的错误:
Traceback (most recent call last):
File "{path_to_code}/so.py", line 17, in <module>
test.grid(column=0, row=0)
File "{path_to_python}\Python35-32\lib\tkinter\__init__.py", line 2072, in grid_configure
self.tk.call(
AttributeError: 'MyClass' object has no attribute 'tk'
我缺少什么?
是的,你说得对。对不起这个错误。 – FamousJameous
正是我所需要的;谢谢:) – bitsmack