1

最近在学习wxPython。所以我用 wxPython 写了一个小的 TCP 服务器程序。按下“init”按钮后,我的程序将崩溃。我还编写了另一个客户端程序,当服务器 GUI 失去响应时,它成功地连接到了这个 wxPython TCP 服务器。这真的让我很困惑。

#!/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(' ')
4

1 回答 1

0

通常,您只需导入另一个模块并实例化该类。像这样的东西会更好

import myFrame

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

或类似的东西。就个人而言,我几乎从不继承 wx.App,除非我打算做一些 PreCreate 的东西。至于为什么您的 GUI 变得无响应,您在ServerInit方法中执行了一个长时间运行的过程,这阻塞了 GUI 的主循环。您需要将套接字连接和通信代码放入单独的线程中,并使用 wxPython 的线程安全方法将结果传回。有关更多信息,请参阅以下链接:

于 2012-11-20T17:19:24.293 回答