我有一个 TCP 服务器和一个 TCP 客户端。
我想使用wxpython 制作下面代码的 GUI 版本。
我已经准备好 GUI 界面脚本,但是在合并这两个脚本时遇到了问题。
如何合并我的套接字脚本和我的 GUI?
我的套接字服务器
from socket import *
from time import ctime
import random
bufsiz = 1024
port = random.randint(1025,36000)
host = 'localhost'
addr = (host, port)
print 'Port:',port
tcpServer = socket(AF_INET , SOCK_STREAM)
tcpServer.bind(addr)
tcpServer.listen(5)
try:
while True:
print 'Waiting for connection..'
tcpClient, caddr = tcpServer.accept()
print 'Connected To',caddr
while True:
data = tcpClient.recv(bufsiz)
if not data:
break
tcpClient.send('[%s]\nData\n%s' % (ctime(),data))
print data
tcpClient.close()
except KeyboardInterrupt:
tcpServer.close()
raw_input('Enter to Quit')
我的 GUI 脚本(使用 wxglade 制作)
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
# generated by wxGlade 0.6.5 (standalone edition) on Mon Feb 18 19:50:59 2013
import wx
# begin wxGlade: extracode
# end wxGlade
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.chat_log = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE | wx.TE_READONLY)
self.text_send = wx.TextCtrl(self, -1, "")
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_TEXT_ENTER, self.text_e, self.text_send)
# end wxGlade
def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle("frame_1")
self.SetSize((653, 467))
self.chat_log.SetMinSize((635, 400))
self.text_send.SetMinSize((635, -1))
self.text_send.SetFocus()
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_1 = wx.FlexGridSizer(1, 1, 1, 1)
sizer_1.Add(self.chat_log, 0, 0, 0)
sizer_1.Add(self.text_send, 0, wx.ALL, 1)
self.SetSizer(sizer_1)
self.Layout()
# end wxGlade
def text_e(self, event): # wxGlade: MyFrame.<event_handler>
text = self.text_send.GetValue()
self.chat_log.AppendText("\n"+text)
self.text_send.SetValue("")
event.Skip()
# end of class MyFrame
class MyMenuBar(wx.MenuBar):
def __init__(self, *args, **kwds):
# begin wxGlade: MyMenuBar.__init__
wx.MenuBar.__init__(self, *args, **kwds)
self.File = wx.Menu()
self.Append(self.File, "File")
self.View = wx.Menu()
self.Append(self.View, "View")
self.__set_properties()
self.__do_layout()
# end wxGlade
def __set_properties(self):
# begin wxGlade: MyMenuBar.__set_properties
pass
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyMenuBar.__do_layout
pass
# end wxGlade
# end of class MyMenuBar
if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()