Python Forum
"tkinter.TclError: NULL main window"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"tkinter.TclError: NULL main window"
#1
Hi, I am creating a quiz as part of an assignment that is set for my course work. Here is my actual code for the quiz:

global score

import tkinter as tk
from tkinter import*
from tkinter import ttk
from tkinter import messagebox
root=Tk()

notebook=ttk.Notebook(root)

frame1=ttk.Frame(notebook)
frame2=ttk.Frame(notebook)
frame3=ttk.Frame(notebook)

notebook.add(frame1,text="Q1A,Q1B,Q1C,Q1D")
notebook.add(frame2,text="Q2")
notebook.add(frame3,text="Q3")
        
quiz=Tk()
quiz.geometry("500x700")
quiz.title("Quiz Code")
quiz.resizable(False,False)
score=0

welcome=tk.Label(quiz,text="Welcome to Quiz")
welcome.pack()



def Inst():
    t=tk.Label(quiz,text="All you have to do is answer each question with either a 'A, B, C, D' or the actual word")
    t.pack()

def Start():    
    Question1=ttk.Label(frame1,text='Q.1.Which anime is Hinata Hyuga from?')
    Question1.grid(row=1,column=0, sticky=W)


    var=StringVar()
    Q1A=ttk.Radiobutton(frame1,text='[A] Watashi ni tenshi ga maiorita',variable=var,value='False1')
    Q1A.grid(row=2,column=0,sticky=W)

    Q1B=ttk.Radiobutton(frame1,text='[B] Oregairu',variable=var,value='False2')
    Q1B.grid(row=3,column=0,sticky=W)

    Q1C=ttk.Radiobutton(frame1,text='[C] Naruto',variable=var,value='True')
    Q1C.grid(row=4,column=0,sticky=W)

    Q1D=ttk.Radiobutton(frame1,text='[D] Overlord', variable=var, value='False3')
    Q1D.grid(row=5,column=0,sticky=W)


    submit=ttk.Button(frame1,text='Submit',command=Answer)
    submit.grid()



def Answer():
    if var.get() == 'True':
        messagebox.showinfo('Congratulations!', message='You have got the answer right!Score is {}'.format(score))
    else:
        messagebox.showinfo('Oops!', message='You have got the answer wrong!')


startButton=tk.Button(quiz,bg="green",fg="black",command=Start,text="Start")
startButton.pack()
instr=tk.Button(quiz,bg="grey",fg="black",command=Inst,text="User Instructions")
instr.pack()
end=tk.Button(quiz,text="Exit",command=quiz.destroy)
end.pack()

quiz.mainloop()
Whenever I try to run my quiz & click on the, I always keep on getting a TCL error stating "NULL main window" Here is the error:

Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Ramaz\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__ return self.func(*args) File "C:\Users\Ramaz\OneDrive\Documents\Mustafa Work\Quiz code.py", line 35, in Start Question1=ttk.Label(frame1,text='Q.1.Which anime is Hinata Hyuga from?') File "C:\Users\Ramaz\AppData\Local\Programs\Python\Python39\lib\tkinter\ttk.py", line 759, in __init__ Widget.__init__(self, master, "ttk::label", kw) File "C:\Users\Ramaz\AppData\Local\Programs\Python\Python39\lib\tkinter\ttk.py", line 557, in __init__ tkinter.Widget.__init__(self, master, widgetname, kw=kw) File "C:\Users\Ramaz\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2568, in __init__ self.tk.call( _tkinter.TclError: NULL main window
Does anyone have a solution to this problem because it's been bugging me for over 2 days now.
buran write Feb-04-2021, 05:47 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
You cannot call Tk() twice. If you need two windows use TopLevel() to make the second window. Tk() doesn't just make a window, it also does a bunch of initialization that is only supposed to happen once.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Tkinter Window Has no Title Bar gw1500se 4 329 Nov-07-2021, 05:14 PM
Last Post: gw1500se
  [PyQt] Can't get MDIarea to resize automatically with Main Window JayCee 4 634 Aug-02-2021, 08:47 PM
Last Post: JayCee
  [Tkinter] check if entry is null rwahdan 2 1,177 Jun-25-2021, 04:24 PM
Last Post: rwahdan
  [Tkinter] _tkinter.TclError: can't invoke "destroy" command: application has been destroyed knoxvilles_joker 6 3,312 Apr-25-2021, 08:41 PM
Last Post: knoxvilles_joker
  [PyQt] How to clip layout to sides and bottom of main window? Valmont 9 1,683 Mar-24-2021, 10:00 PM
Last Post: deanhystad
  function in new window (tkinter) Dale22 7 1,336 Nov-24-2020, 11:28 PM
Last Post: Dale22
  Scrollable big image in a window (TKinter) Prospekteur 3 1,235 Sep-14-2020, 03:06 AM
Last Post: Larz60+
  Run more than one window together with tkinter SmukasPlays 1 782 Aug-24-2020, 05:00 PM
Last Post: Larz60+
  Adding an image to a tkinter window djwilson0495 2 3,812 Aug-23-2020, 11:07 AM
Last Post: ebolisa
  [Tkinter] _tkinter.TclError: bitmap "Icon.gif" not defined djwilson0495 2 5,074 Aug-05-2020, 02:27 PM
Last Post: wuf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020