《Python程序设计》习题与答案 联系客服

发布时间 : 星期一 文章《Python程序设计》习题与答案更新完毕开始阅读

app = wxGUI() app.MainLoop()

9.2 设计一个窗体,并放置一个按钮,按钮默认文本为“开始”,单击按钮后文本变为“结束”,再次单击后变为“开始”,循环切换。 import wx

class wxGUI(wx.App): def OnInit(self):

frame = wx.Frame(parent=None, title='wxGUI', size=(160,140)) panel = wx.Panel(frame, -1)

self.buttonOK = wx.Button(panel, -1, 'Start', pos=(0,0)) self.Bind(wx.EVT_BUTTON, self.OnButtonOK, self.buttonOK) frame.Show() return True

def OnButtonOK(self, event): if text == 'Start': elif text == 'End': app = wxGUI()

app.MainLoop()

9.3 设计一个窗体,模拟QQ登录界面,当用户输入号码123456和密码654321时提示正确,否则提示错误。 import wx

class wxGUI(wx.App): def OnInit(self):

frame pos=(350,350))

= wx.Frame(parent=None, title='Login', size=(250,150),

panel = wx.Panel(frame, -1)

label1 = wx.StaticText(panel, -1, 'UserName:', pos=(0,10),

style=wx.ALIGN_RIGHT)

label2 = wx.StaticText(panel, -1, 'Password:', pos=(0,30),

style=wx.ALIGN_RIGHT)

self.textName = wx.TextCtrl(panel, -1, pos=(70,10), size=(160,20))

self.textPwd = wx.TextCtrl(panel, -1, pos=(70,30),

size=(160,20),style=wx.TE_PASSWORD)

buttonOK = wx.Button(panel, -1, 'OK', pos=(30,60)) self.Bind(wx.EVT_BUTTON, self.OnButtonOK, buttonOK)

buttonCancel = wx.Button(panel, -1, 'Cancel', pos=(120,60)) self.Bind(wx.EVT_BUTTON, self.OnButtonCancel, buttonCancel) buttonOK.SetDefault() frame.Show() return True

def OnButtonOK(self, event):

if usrName=='123456' and usrPwd=='654321': wx.MessageBox('Right') else:

wx.MessageBox('Wrong') def OnButtonCancel(self, event): pass app = wxGUI() app.MainLoop()

第10章 网络程序设计

10.1 简单解释TCP和UDP协议的区别。 答:

TCP协议是面向连接的、具有质量保证的可靠传输协议,但开销较大;UDP协议是尽最大能力传输的无连接协议,开销小,常用于视频在线点播(Video On Demand, VOD)之类的应用。TCP协议和UDP协议并没有优劣之分,仅仅是适用场合有所不同。

10.2 同学之间合作编写UDP通信程序,分别编写发送端和接收端代码,发送端发送一个字符串“Hello world!”。假设接收端在计算机的5000端口进行接收,并显示接收内容。

答:首先使用ipconfig/all命令查看本机IP地址,然后分别编写下面的代码,并将其中的IP地址替换为相应的IP地址。

接收端代码: import socket

s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind((\空字符串表示本机任何可用IP地址 data, addr=s.recvfrom(1024) # 缓冲区大小为1024字节 print ' received message:%s' % data #显示接收到的内容 s.close( )

发送端代码: