远程计算器控制(包含wxPython,socket,thread)(初级水平)

其实很简单,只因我是新手,所以浪费了些时间。

客户端代码:

#coding=utf8
#这个是主线程加子线程的
import wx
import math
from socket import *
import threading
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):
        t_Calc=threading.Thread(target=Conncal,args=(self,event))
        t_Calc.setDaemon(True)
        t_Calc.start()  
        
    def OnClick(self, event):
        self.resultStr += self.buttonLabels[event.GetId()]
        self.setText(self.resultStr)
        event.Skip()


def Conncal(FrameObject, event):
#         result="Error"
    result=FrameObject.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)
        print result    #在客户端控制台输出
    except:
        pass
    FrameObject.resultStr=result
    FrameObject.setText(result)
    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()


完。(原创作品,转载请注明出处)

posted @ 2022-04-29 22:37  WORDLESS  阅读(47)  评论(0编辑  收藏  举报