Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8863629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:01:41+00:00 2026-06-14T16:01:41+00:00

Recently, I’m learning wxPython. So I wrote a little TCP server program with wxPython.

  • 0

Recently, I’m learning wxPython. So I wrote a little TCP server program with wxPython. My program will be crashed after I pressed the ‘init’ button. I also wrote another client program which successfully connected with this wxPython TCP server when server GUI lost response.
This really confused me.

#!/usr/bin/env python
#Boa:App:BoaApp
#coding:utf-8

import wx

import Frame1

modules ={'Frame1': [1, 'Main frame of Application', u'Frame1.py']}

class BoaApp(wx.App):
    def OnInit(self):
        self.main = Frame1.create(None)
        self.main.Show()
        self.SetTopWindow(self.main)
        return True

def main():
    application = BoaApp(0)
    application.MainLoop()

if __name__ == '__main__':
    main()

#

#Boa:Frame:Frame1
#coding:utf-8

import wx
import socket
import sys
import time

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1BUTTON2, wxID_FRAME1PANEL1, 
 wxID_FRAME1STATICLINE1, wxID_FRAME1STATICTEXT1, wxID_FRAME1STATICTEXT2, 
 wxID_FRAME1TEXTCTRL1, wxID_FRAME1TEXTCTRL2, wxID_FRAME1TEXTCTRL3, 
] = [wx.NewId() for _init_ctrls in range(10)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(644, 336), size=wx.Size(302, 375),
              style=wx.DEFAULT_FRAME_STYLE & ~ wx.RESIZE_BORDER, 
              title=u'TCP SERVER')                     
        self.SetClientSize(wx.Size(286, 337))

        self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(286, 337),
              style=wx.TAB_TRAVERSAL)

        self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
              label=u'HOST', name='staticText1', parent=self.panel1,
              pos=wx.Point(32, 24), size=wx.Size(80, 24), style=0)

        self.staticText2 = wx.StaticText(id=wxID_FRAME1STATICTEXT2,
              label=u'PORT', name='staticText2', parent=self.panel1,
              pos=wx.Point(32, 64), size=wx.Size(72, 22), style=0)

        self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
              parent=self.panel1, pos=wx.Point(136, 24), size=wx.Size(136, 22),
              style=0, value=u'ENTER HOSTNAME')

        self.textCtrl2 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL2, name='textCtrl2',
              parent=self.panel1, pos=wx.Point(136, 64), size=wx.Size(136, 22),
              style=0, value=u'ENTER PORT')

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label=u'INIT',
              name='button1', parent=self.panel1, pos=wx.Point(48, 112),
              size=wx.Size(75, 24), style=0)

        self.button2 = wx.Button(id=wxID_FRAME1BUTTON2, label=u'CLEAR',
              name='button2', parent=self.panel1, pos=wx.Point(168, 112),
              size=wx.Size(75, 24), style=0)

        self.staticLine1 = wx.StaticLine(id=wxID_FRAME1STATICLINE1,
              name='staticLine1', parent=self.panel1, pos=wx.Point(16, 152),
              size=wx.Size(256, 2), style=0)

        self.textCtrl3 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL3, name='textCtrl3',
              parent=self.panel1, pos=wx.Point(32, 168), size=wx.Size(232, 144),
              style=wx.TE_MULTILINE, value=u'MESSAGE')  #STYLE CHANGED

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.Bind(wx.EVT_BUTTON, self.ServerInit, self.button1)
        self.Bind(wx.EVT_BUTTON, self.CleanUp, self.button2) 

    def ServerInit(self, event):
        self.textCtrl3.ChangeValue('')
        self.HOST = ''      
        self.PORT = 8888    
        #self.GetValue()
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.textCtrl3.AppendText('Socket created\n')

        try:
            self.s.bind((self.HOST, self.PORT))
        except socket.error , msg:
            self.textCtrl3.AppendText('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] + '\n')
            sys.exit(1)

        self.textCtrl3.AppendText('Socket bind complete\n')

        self.s.listen(10)
        self.textCtrl3.AppendText('Socket now listening\n')

        #now keep talking with the client
        while (1):
            #wait to accept a connection - blocking call
            conn, addr = self.s.accept()
            tempStr = 'Connected with ' + addr[0] + ':' + str(addr[1]) + '\n'
            self.textCtrl3.AppendText(tempStr)

            while (1):
                data = conn.recv(1024)
                reply = 'RECEIVED:' + data + '\n'
                self.textCtrl3.AppendText(reply)
                conn.sendall('SERVER RECEIVED')
        conn.sendall('DISCONNECTED')
        conn.close()
        s.close()


    def CleanUp(self, event):
        self.textCtrl3.ChangeValue(' ')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:01:42+00:00Added an answer on June 14, 2026 at 4:01 pm

    Normally, you just import the other module and instantiate the class. Something like this would be better

    import myFrame
    
    # then in your OnInit
    def OnInit(self):
       frame = myFrame.Frame1()
    

    Or something similar. Personally, I almost never subclass wx.App unless I plan to do some PreCreate stuff. As for why your GUI becomes unresponsive, you are doing a long running process in your ServerInit method which is blocking the GUI’s main loop. You’ll want to put your socket connection and communication code into a separate thread and pass the results back using wxPython’s thread-safe methods. See the following links for more info:

    • http://wiki.wxpython.org/LongRunningTasks
    • http://www.blog.pythonlibrary.org/wxpython-and-threads
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently our WPF/Entity Framework 4.0 application has become unstable after we began using backgroundworkers
Recently in a technical interview, I was asked to write a program to find
Recently, I wrote a small and functionally CMS, without OOP logic, and with the
Recently I've started working on project VB for my As computing class, and after
Recently (that is in winter in few days) I wrote a simple script which
Recently I started learning CoreData. Figured out how to create a database, create a
Recently learning Android, there is a Handler class in android which is very useful.
Recently wrote some JavaScript code, but there has been a memory leak in IE7.Are
Recently there was some upgrade happened from frame work 2.0 to 4.0, so after
Recently, our .Net client libaray is upgrading to compile against Net 4.0. After change

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.