AttributeError: 'New_Question' object has no attribute 'Tk'

AttributeError: 'New_Question' object has no attribute 'Tk'

我正在尝试制作一个简单的对话框,并不断收到 AttributeError。
这是代码:

import tkinter
from tkinter import ttk
import tkinter.font as font
import tkinter as tk
from Question import *
import pickle

class New_Question:
    #creating a root
    def __init__(self, category):
        self.cat = category
        self.qRoot = tkinter.Tk()

    def add_question(self):

        self.lblPrompt = self.Tk.Label(self.qRoot, text="Please enter a question for"+ self.cat)
        self.lblPromt.pack()
        self.entQuestion = self.Tk.Entry (self.qRoot)
        self.lbl.entQuestion.pack()

        self.lblAnswer = self.Tk.Label(self.qRoot, text="Please enter the answer:")
        self.lblAnswer.pack()
        self.entAnswer = self.Tk.Entry (self.qRoot )
        self.entAnswer.pack()

        self.q = question(self.qtext, self.qanswer)        
        self.qRoot.mainloop()
        return self.q

我只是想让它调出一个带有小部件的 tkinter window。 任何帮助将不胜感激。

错误完美地描述了问题。在您的 __init__ 函数中,您没有定义任何 self.Tk。您只需要使用 tk。 您还多次导入 tkinter,这是您不应该做的。删除 import tkinterimport tkinter as tk。 更常见的是 from tkinter import *,它允许您省略 tkinter.tk.。 这是一个改进的程序(未测试):

from tkinter import *
from Question import *
import pickle

class New_Question:
    #creating a root
    def __init__(self, question, answer, category):
        self.qRoot = Tk()
        self.lblPrompt = Label(self.qRoot, text="Please enter a question for "+ category)
        self.lblPrompt.pack()
        self.entQuestion = Entry(self.qRoot)
        self.entQuestion.pack()

        self.lblAnswer = Label(self.qRoot, text="Please enter the answer:")
        self.lblAnswer.pack()
        self.entAnswer = Entry (self.qRoot )
        self.entAnswer.pack()

        q = question(question, answer)
    def run():
        self.qRoot.mainloop()