远程计算器控制(包含wxPython,socket)(初级水平)
就俩文件,一个客户端,一个服务器
这是客户端代码:
#coding=utf8
#本文的原型是一个简易计算器,是在此基础上改过来的
#这个是仅仅有一个主线程
#这是客户端,客户端把文本框里的内容发送给服务器,服务器计算后把结果发给客户端
import wx
import math
from socket import *
class MainFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Calculator', size=(220,400),
style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX))
panel = wx.Panel(self)
gridSizer = wx.GridSizer(rows=4, cols=4, hgap=3, vgap=3)
mainSizer = wx.BoxSizer(wx.VERTICAL)
self.resultStr = ''
#-----StatusBar--------------------------------
self.statusBar = wx.StatusBar(self, -1)
self.statusBar.SetStatusText('Welcome to use this Calculator!')
#----StaticText-----------------------------------
self.staticText = wx.StaticText(panel, -1, '')
#----TextCtrl----------------------------
self.textCtrl = wx.TextCtrl(panel, -1, '',style=wx.TE_MULTILINE|wx.TE_RICH2)
font = wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD, underline=False)
self.textCtrl.SetFont(font)
#-------Buttons-------------------------------------------------
self.buttonLabels = "7 8 9 + 4 5 6 - 1 2 3 * 0 . / =".split()
self.buttons = []
self.flag = [] #index of button
btnLength = len(self.buttonLabels)
for i in range(btnLength):
self.buttons.append(wx.Button(panel, i, size=(50,50),label=("%s" % self.buttonLabels[i])))
gridSizer.Add(self.buttons[i], 0, 0)
mainSizer.Add(self.textCtrl, 1, wx.EXPAND)
mainSizer.Add(self.staticText, 0.7, wx.EXPAND)
mainSizer.Add(gridSizer, 4, wx.EXPAND)
panel.SetSizerAndFit(mainSizer)
self.Bind(wx.EVT_BUTTON, self.OnResult, self.buttons[-1])
#a list of handlers
self.funcList = [self.OnClick]*(btnLength-1)
for i in range(btnLength-1):
self.buttonBind(i)
def setText(self, value):
'''set the value of TextCtrl'''
self.textCtrl.SetValue(value)
def buttonBind(self, index):
self.buttons[index].Bind(wx.EVT_LEFT_DOWN, self.funcList[index])
def OnResult(self, event):
# result="Error"
result=self.resultStr
try:
HOST,PORT="127.0.0.1",56788
s = socket(AF_INET, SOCK_STREAM)
s.connect((HOST, PORT))
s.send(result + '=')
result=s.recv(1024)
except:
pass
self.resultStr=result
self.setText(result)
event.Skip()
def OnClick(self, event):
self.resultStr += self.buttonLabels[event.GetId()]
self.setText(self.resultStr)
event.Skip()
if __name__=='__main__':
app = wx.PySimpleApp()
frame = MainFrame(None, -1)
frame.Show()
app.MainLoop()
这是服务器端代码
#coding=utf8
#这是服务器,服务器接收客户端发来的算式并计算,把结果发回去
import SocketServer
class TelnetProtocolHandler(SocketServer.StreamRequestHandler):
def setup(self):
SocketServer.StreamRequestHandler.setup(self)
#Something
def handle(self):
#if line != "" :
self.data = self.request.recv(1024).strip()
t=self.data
if t.endswith('='):
t=t[:-1]
self.request.send(str(eval(t)))#计算核心代码
consoleshow=t+"="+str(eval(t))#服务器端控制台打印算式
print consoleshow
else:
#Do somthing
print t
def finish(self):
SocketServer.StreamRequestHandler.finish(self)
# self.server.finishConnection(self)
socServer = SocketServer.ThreadingTCPServer(("127.0.0.1",56788), TelnetProtocolHandler)
socServer.serve_forever()
完。(原创作品,转载请注明出处)