0

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.

nerdguy
  • 174
  • 1
  • 16
POVEY
  • 27
  • 6

1 Answers1

0

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()
nerdguy
  • 174
  • 1
  • 16
  • I think the line `q = question(question, answer)` may raise exception if the passed argument `question` is not a callable object or a class. – acw1668 Mar 17 '21 at 15:19
  • Even though wildcard import may seem to be *more common*, it isn't something that's recommended. – astqx Mar 17 '21 at 15:20
  • @acw1668 I can't figure out what `question` is, as it is not defined anywhere in the code. – nerdguy Mar 17 '21 at 15:20
  • @AST But for beginner projects, it makes it easier – nerdguy Mar 17 '21 at 15:21
  • 1
    `question` may be a class in `Question` module in OP design, but your solution is an argument of `__init__()`. – acw1668 Mar 17 '21 at 15:22
  • @acw1668 I know, the argument is supposed to be a string. Again, I don't know how to use `Question`. – nerdguy Mar 17 '21 at 15:24
  • Then why does your solution add the arguments `question` and `answer` to `__init__()` that OP code does not have? – acw1668 Mar 17 '21 at 15:25
  • In order to clean up the code. It needed values to come from *somewhere*. – nerdguy Mar 17 '21 at 15:26
  • @nerdguy thanks for the answer. I am new to coding and it was a great help. I imported "Question" because I have another script called "Question" and I forgot to take out the irrelevant imports. also your advise on importing tkinter is very helpful and i will use it going forward. – POVEY Mar 19 '21 at 11:41