Search code examples
pythontkintertk-toolkitattributeerror

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


I am trying to make a simple dialog box, and keep getting an AttributeError.
Here is the code:

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

I just want it to bring up a tkinter window with the widgets. Any help would be appreciated.


Solution

  • The error perfectly describes the problem. In your __init__ function, you do not define any self.Tk. You need to use just tk. You also import tkinter multiple times, which you should not do. Either remove the import tkinter or the import tkinter as tk. More common is from tkinter import *, which allows you to leave out the tkinter. or tk.. Here is an improved program (not tested):

    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()