Error: Can't create window of class wxWindowClassNR (error 1407: cannot find window class.)

Im getting the above error and while I can still launch my program its connected to my socket thread some how.... Why am I getting it ? What does it mean and how can I fix it ? If it matters I'm using wxpython ... do I need to import a module or something ?

class ThreadClass(threading.Thread):
    def run(self):
	f =open("ipaddress.txt","r")
	self.ip = f.readline() # reads the lines
	print self.ip
        while True:
	    #print 'attempt'
	    ntwrk=open ("network.txt","a")
	    ntwrk.write('handle on connection attempt')
            #print "handle one connection attempt"
            success = self.run_once()
            if success:
		#print "success"
		#print 'Success'
		ntwrk=open ("network.txt","a")
               # do whatvever is appropriate
		#print "fail"
		#print 'failed'
		ntwrk=open ("network.txt","a")
               # do whatever is appropriate
		time.sleep(20) # adapt the delay 

    def run_once(self):
	#print host
        port = 51269
        size = 1000000
        addr = (host,port)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            f = open('duplicate_table2.txt', "r")
            data =         
            data = s.recv(size)
            #print "Got it"
            return True
        except socket.error:  
	    ntwrk=open ("network.txt","w")
	    ntwrk.write('socket error')
            #print "socket error"
        except IOError:
	    ntwrk=open ("network.txt","w")
            #print "IOError"
        return False

Open in new window

Who is Participating?
gelonidaConnect With a Mentor Commented:
Np problem if you're still stuck or want to get more info, then just contact me again  and send m a little more info like

Perhaps you run out of some resources (memories / file handles, . ..  )
- What OS are you using?
- Did you check memory / cpu usage (or does your program die when the error occurs and you cannot look at CPU / memory consumption)\
- does the porblem also occur without the mailer thread
- does the problem occur if you let all your threads sleep for a few seconds (and the window is thus created before all the thredas start their real activity)

I have difficulties believing, that the issue is related to above code.
Perhaps you accidentally changed something else in the wxWidgets related code?

Couldn't you just comment out the entire Thread class and rerun your program.

Probably you  should show us the backtrace message and the related lines of code of the Wxwidgets code.

dolamitejenkinsAuthor Commented:
that's what is so confusing it does do that... almost like its not a python error but a windows os error,,, that is the entire error message  that occurs in the command window... it doesn't always appears only sometimes ... when it appears the network thread does not work... any thoughts
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

Some suggestions.

Create a small complete test case
Could you try to create the smallest program possible, that is still producing this error?
Mostly it is much easier to identify the problem.
and sometimes by coincidence you fall over the exact piece of code causing the problem

Look at the process status
Could you use the Process manager under windows (or ps  or top under Linux)

and Look at:
- the cpu load of your non working program
- the memory requirements of your application

Ideally you look at these values while your program is still working and lateron
when it is already dead.

Tell u sthe OS you're running on

Are you running under Linux or under Windows?
This information can help to suggest some debug strategies.

Uncomment the print statments and tell us where the socket thread dies / stops

you could uncomment the print statements and tell us which is the last print statement of
your socket thread.

Is it always the same????

Could you also add print statements in the wxWIdgets thread?

Tell us a little more about your program
I don't really care about the exact thing your program is doing, but some information might be helpful.

For example.
- Do you create any window when your program starts up.
- Do you create new windows  as reaction to the socket thread
- how do the socket thread and your WxWidget main loop communicate with each other (queues / events / . . . )

- Do you create more threads than the wxWidgets main lop and the socket thread?

dolamitejenkinsAuthor Commented:
I have 3 loops running one timer thread , the socket thread and then a email thread...  I do create a window on start up ... what is unusual is the error occurs on start up of the program... so when all the thread are initiated... im gonna do the other things you suggest and post when I have a the results ...
dolamitejenkinsAuthor Commented:
the code I posted is causing the error ... I commented it out and started the program 50 times no error and I un-commented it and right away the error reappeared .... is there something with threads or sockets and wxpython that I should be aware of ?
dolamitejenkinsAuthor Commented:
I think I may have solved it by deleting wx.ContextHelpButton ... haven't had the error since ... have you heard of this ? why is this possibly the problem ?
No I don't have any idea. I never used wxWidgets :-( .

So to summarize:
- You have one socket thread
- you have one mailer thread
- you have one timer Thread (What is a timer thread ) Or do you start multiple timer threads within your loops???
No other thread is created later on

The window creation fails at startup (it fails only sometimes)

Perhaps you run out of some resources (memories / file handles, . ..  )
- What OS are you using?
- Did you check memory / cpu usage (or does your program die when the error occurs and you cannot look at CPU / memory consumption)

Could you create the window BEFORE your threads do any actual work? (You could just add a sleep() statement at the beginning of each thread, such, that you're sure the window is created before your threads start using memory / CPU?
Does the problem still occur if you don't start the mailer thread. (The less code you have to reproduce the problem the easier it is to idnetify)

Some minor comments on your code  (not explaining the bug).
- Sometimes you open 'network.txt' with 'a' mode (append) and sometimes with 'w' (create)
    Is this intentionally?
- On someplaces your code uses ntwrk.close . This hsould be ntwrk.close() with parenthesis.
    Otherwise the code has no effect.
- Your code might be nicer to read if you explicitely checked for the existence of the file to send

You could replace line 43
f = open('duplicate_table2.txt', "r")
fname = 'duplicate_table2.txt',
if not os.path.exists(fname):
    # log a message here if you want
    return False
f = open(fname, "r")

Instead of always repeating three lines of code you might create a logging function:


def log_msg(fname, mode, message):
    f = open(fname, mode)
    write(f, message)

Your code will be shorter and easier to read.

Alternatively I would look at the python logging module It is very flexible and very configurable:
A simple example of using it is:
# Add these lines at the beginning of your file:
import logging
netlogger = logging.getLogger('network')
netlogger.addHandler(logging.FileHandler('network.txt', mode='w'))
netlogger.propagate = False

Later in your code just use lines like following examples.'sucess')
netlogger.error('socket error')
netlogger.warning('file %s does not exist' % fname)

dolamitejenkinsAuthor Commented:
thank you
dolamitejenkinsAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.