Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2011-09-26
Medium Priority
Last Modified: 2012-05-12
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 = f.read()         
            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

Question by:dolamitejenkins
  • 6
  • 4
LVL 17

Expert Comment

ID: 36708081

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.


Author Comment

ID: 36712782
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
LVL 17

Expert Comment

ID: 36713312
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?

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 36713510
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 ...

Author Comment

ID: 36714636
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 ?

Author Comment

ID: 36714821
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 ?
LVL 17

Expert Comment

ID: 36715727
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.
netlogger.error('socket error')
netlogger.warning('file %s does not exist' % fname)


Author Comment

ID: 36717145
thank you
LVL 17

Accepted Solution

gelonida earned 2000 total points
ID: 36718028
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)

Author Closing Comment

ID: 36773840

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question