作业信息
[2021-2022-1信息安全专业导论](https://edu.cnblogs.com/campus/besti/2021-2022-1fois)
[2021-2022-1信息安全专业导论第十周作业](https://www.cnblogs.com/rocedu/p/9577842.html#WEEK10)
教材学习内容总结
- 阅读《计算机科学概论》第12,13,14章(信息系统,人工智能,模拟、图形学、游戏和其他应用),了解了信息管理,电子制表软件,数据库管理系统,电子商务,思维机,知识表示,专家系统,神经网络,自然语言处理,机器人学,模拟,特殊模型,计算机图形学,游戏等内容。
- 阅读《看漫画学Python》第13、15章(图形用户界面、访问数据库),安装了wxPython和SQLite,了解了自定义窗口,在窗口中添加控件,事件处理,布局管理,控件,数据库编程的基本操作过程,sqlite3模块API,数据库的CRUD的操作实例,防止SQL注入攻击等内容。
教材学习中的问题和解决过程
问题1:根据教材的方法无法下载wxPython,会显示:WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, st…的错误信息
问题1解决方案:到wxPython官方网站查询,找到了新的下载命令。
代码调试过程中的问题和解决过程
问题1:本周学习任务的代码有点多,每个部分代码也比较长,写的时候容易出现各种错误,包括串行、拼写等的问题
问题1解决方案: 在调试时注意看错误提示语,加以修改。写代码的时候要细心
代码托管
import wx
app = wx.App()
frm = wx.Frame(None,title="第一个wxPython程序!",size = (400,300),pos = (100,100))
frm.Show()
app.MainLoop()
import wx
class MyFrame(wx.Frame):
def init(self):
super().init(None,title="第一个wxPython程序!",size=(400,300),pos=(100,100))
app = wx.App()
frm = MyFrame()
frm.Show()
app.MainLoop()
import wx
from wx.core import StaticText
class MyFrame(wx.Frame):
def init(self):
super().init(None,title="第一个wxPython程序!",size=(400,300),pos=(100,100))
panel = wx.Panel(parent=self)
statictext = wx.StaticText(parent = panel,label='Hello World',pos = (10,10))
app = wx.App()
frm = MyFrame()
frm.Show()
app.MainLoop()
import wx
class MyFrame(wx.Frame):
def init(self):
super().init(None,title="事件处理",size=(300,100))
panel = wx.Panel(parent=self)
self.statictext = wx.StaticText(parent=panel,label="请单击OK按钮",pos = (110,20))
b = wx.Button(parent = panel,label = 'OK',pos = (100,50))
self.Bind(wx.EVT_BUTTON,self.on_click,b)
def on_click(self,event):
self.statictext.SetLabelText('Hello,World.')
app = wx.App()
frm = MyFrame()
frm.Show()
app.MainLoop()
import wx
class MyFrame(wx.Frame):
def init(self):
super().init(None,title="事件处理",size=(300,100))
panel = wx.Panel(parent=self)
self.statictext = wx.StaticText(parent=panel,label="请单击OK按钮")
b = wx.Button(parent = panel,label = 'OK')
self.Bind(wx.EVT_BUTTON,self.on_click,b)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(self.statictext,proportion=1,
flag=wx.ALIGN_CENTER_HORIZONTAL|wx.FIXED_MINSIZE|wx.Top,border=30)
vbox.Add(b,proportion=1,flag=wx.EXPAND|wx.BOTTOM,border=10)
panel.SetSizer(vbox)
def on_click(self,event):
self.statictext.SetLabelText('Hello,World.')
app = wx.App()
frm = MyFrame()
frm.Show()
app.MainLoop()
import wx
class MyFrame(wx.Frame):
def init(self):
super().init(None,title="布局管理器嵌套",size=(300,120))
panel = wx.Panel(parent=self)
self.statictext = wx.StaticText(parent=panel,label="请单击按钮")
b1 = wx.Button(parent = panel,id = 10,label = 'Button1')
b2 = wx.Button(parent = panel,id = 11,label = 'Button2')
hbox = wx.BoxSizer(wx.HORIZONTAL)
hbox.Add(b1,proportion=1,flag=wx.EXPAND|wx.ALL,border=10)
hbox.Add(b2,proportion=1,flag=wx.EXPAND|wx.ALL,border=10)
vbox= wx.BoxSizer(wx.VERTICAL)
vbox.Add(self.statictext,proportion=1,
flag=wx.CENTER|wx.FIXED_MINSIZE|wx.Top,border=10)
vbox.Add(hbox,proportion=1,flag=wx.CENTER)
panel.SetSizer(vbox)
self.Bind(wx.EVT_BUTTON,self.on_click,id=10,id2=20)
def on_click(self,event):
event_id = event.GetId()
print(event_id)
if event_id == 10:
self.statictext.SetLabelText('Button1单击')
else:
self.statictext.SetLabelText('Button2单击')
app = wx.App()
frm = MyFrame()
frm.Show()
app.MainLoop()
import wx
class MyFrame(wx.Frame):
def init(self):
super().init(None,title="文本输入控件",size=(300,260))
panel = wx.Panel(parent=self)
tc1 = wx.TextCtrl(panel)
tc2 = wx.TextCtrl(panel,style=wx.TE_PASSWORD)
tc3 = wx.TextCtrl(panel,style=wx.TE_MULTILINE)
userid = wx.StaticText(panel,label="用户ID: ")
pwd = wx.StaticText(panel,label="密码: ")
content = wx.StaticText(panel,label="多行文本: ")
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(userid,flag=wx.EXPAND|wx.LEFT,border=10)
vbox.Add(tc1,flag=wx.EXPAND|wx.ALL,border=10)
vbox.Add(pwd,flag=wx.EXPAND|wx.LEFT,border=10)
vbox.Add(tc2,flag=wx.EXPAND|wx.ALL,border=10)
vbox.Add(content,flag=wx.EXPAND|wx.LEFT,border=10)
vbox.Add(tc3,flag=wx.EXPAND|wx.ALL,border=10)
panel.SetSizer(vbox)
tc1.SetValue('tony')
print('读取用户ID:{0}'.format(tc1.GetValue()))
app = wx.App()
frm = MyFrame()
frm.Show()
app.MainLoop()
import wx
class MyFrame(wx.Frame):
def init(self):
super().init(None,title="复选框和单选按钮",size=(330,120))
panel = wx.Panel(parent=self)
st1 = wx.StaticText(panel,label = '选择你喜欢的编程语言: ')
cb1 = wx.CheckBox(panel,id = 1,label = 'Python')
cb2 = wx.CheckBox(panel,id = 2,label = 'Java')
cb2.SetValue(True)
cb3 = wx.CheckBox(panel,id = 3,label = 'C++')
self.Bind(wx.EVT_CHECKBOX, self.on_checkbox_click,id=1,id2=3)
st2 = wx.StaticText(panel,label='选择性别: ')
radio1 = wx.RadioButton(panel,id=4,label='男',style=wx.RB_GROUP)
radio2 = wx.RadioButton(panel,id=5,label='女')
self.Bind(wx.EVT_RADIOBUTTON,self.on_radio_click,id=4,id2=5)
hbox1 = wx.BoxSizer()
hbox1.Add(st1,flag=wx.LEFT|wx.RIGHT,border=5)
hbox1.Add(cb1)
hbox1.Add(cb2)
hbox1.Add(cb3)
hbox2 = wx.BoxSizer()
hbox2.Add(st2,flag=wx.LEFT|wx.RIGHT,border=5)
hbox2.Add(radio1)
hbox2.Add(radio2)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox1,flag=wx.ALL,border=10)
vbox.Add(hbox2,flag=wx.ALL,border=10)
panel.SetSizer(vbox)
def on_checkbox_click(self,event):
cb = event.GetEventObject()
print("选择{0},状态{1}".format(cb.GetLabel(),event.IsChecked()))
def on_radio_click(self,event):
rb = event.GetEventObject()
print("第一组{0}被选中".format(rb.GetLabel()))
app = wx.App()
frm = MyFrame()
frm.Show()
app.MainLoop()
import wx
class MyFrame(wx.Frame):
def init(self):
super().init(None,title="列表",size=(350,175))
panel = wx.Panel(parent=self)
st1 = wx.StaticText(panel,label = '选择你喜欢的编程语言: ')
list1 = ['Python','C++','Java']
lb1 = wx.ListBox(panel,choices=list1,style = wx.LB_SINGLE)
self.Bind(wx.EVT_LISTBOX, self.on_listbox1,lb1)
st2 = wx.StaticText(panel,label='选择你喜欢吃的水果: ')
list2 = ['苹果','橘子','香蕉']
lb2 = wx.ListBox(panel,choices=list2,style = wx.LB_EXTENDED)
self.Bind(wx.EVT_LISTBOX, self.on_listbox2,lb2)
hbox1 = wx.BoxSizer()
hbox1.Add(st1,proportion=1,flag=wx.LEFT|wx.RIGHT,border=5)
hbox1.Add(lb1,proportion=1)
hbox2 = wx.BoxSizer()
hbox2.Add(st2,proportion=1,flag=wx.LEFT|wx.RIGHT,border=5)
hbox2.Add(lb2,proportion=1)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox1,flag=wx.ALL|wx.EXPAND,border=5)
vbox.Add(hbox2,flag=wx.ALL|wx.EXPAND,border=5)
panel.SetSizer(vbox)
def on_listbox1(self,event):
listbox = event.GetEventObject()
print("选择{0}".format(listbox.GetSelection()))
def on_listbox2(self,event):
listbox = event.GetEventObject()
print("选择{0}".format(listbox.GetSelections()))
app = wx.App()
frm = MyFrame()
frm.Show()
app.MainLoop()
import wx
class MyFrame(wx.Frame):
def init(self):
super().init(None,title="静态图片控件",size=(300,300))
panel = wx.Panel(parent=self)
self.bmps = [wx.Bitmap('images/bird5.gif',wx.BITMAP_TYPE_GIF),
wx.Bitmap('images/bird4.gif',wx.BITMAP_TYPE_GIF),
wx.Bitmap('images/bird3.gif',wx.BITMAP_TYPE_GIF)]
b1 = wx.Button(self.panel,id = 1,label="Button1")
b2 = wx.Button(self.panel,id = 2,label="Button2")
self.Bind(wx.EVT_BUTTON,self.on_click,id = 1,id2 = 2)
self.image = wx.StaticBitmap(self.panel,bitmap = self.bmps[0])
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(b1,proportion=1,flag=wx.ALL|wx.EXPAND)
vbox.Add(b2,proportion=1,flag=wx.ALL|wx.EXPAND)
vbox.Add(self.image,proportion=3,flag=wx.EXPAND)
self.panel.SetSizer(vbox)
def on_click(self,event):
event_id=event.GetId()
if event_id == 1:
self.image.SetBitmap(self.bmps[1])
else:
self.image.SetBitmap(self.bmps[2])
self.panel.Layout()
app = wx.App()
frm = MyFrame()
frm.Show()
app.MainLoop()
import sqlite3
try:
con = sqlite3.connect('school_db.db')
cursor = con.cursor()
sql = 'SELECT s_id,s_name,s_sex,s_birthday FROM student'
cursor.execute(sql)
result_set = cursor.fetchall()
for row in result_set:
print("学号:{0} - 姓名:{1} - 性别:{2} - 生日:{3}".format(row[0],row[1],row[2],row[3]))
except sqlite3.Error as e:
print('数据查询发生错误:{}'.format(e))
finally:
if cursor:
cursor.close()
if con:
con.close()
import sqlite3
i_name = input('请输入【姓名】: ')
i_sex = input('请输入【性别】(1表示男,0表示女): ')
i_birthday = input('请输入【生日】(yyyyMMdd): ')
try:
con = sqlite3.connect('school_db.db')
cursor = con.cursor()
sql = 'INSERT INTO student (s_name,s_sex,s_birthday) VALUES(?,?,?)'
cursor.execute(sql,[i_name,i_sex,i_birthday])
con.commit()
print('插入数据成功。')
except splite3.Error as e:
print('插入数据失败:{}'.format(e))
con.rollback()
finally:
if cursor:
cursor.close()
if con:
con.close()
import sqlite3
i_id = input("请输入【学号】: ")
i_name = input('请输入【姓名】: ')
i_sex = input('请输入【性别】(1表示男,0表示女): ')
i_birthday = input('请输入【生日】(yyyyMMdd): ')
try:
con = sqlite3.connect('school_db.db')
cursor = con.cursor()
sql = 'UPDATE student SET s_name=?,s_sex=?,s_birthday=?WHERE s_id=?'
cursor.execute(sql,[i_name,i_sex,i_birthday,i_id])
con.commit()
print('更新数据成功。')
except sqlite3.Error as e:
print('更新数据失败:{}'.format(e))
con.rollback()
finally:
if cursor:
cursor.close()
if con:
con.close()
import sqlite3
i_id = input("请输入【学号】: ")
try:
con = sqlite3.connect('school_db.db')
cursor = con.cursor()
sql = 'DELETE FROM student WHERE s_id=?'
cursor.execute(sql,[i_id])
con.commit()
print('删除数据成功。')
except sqlite3.Error as e:
print('删除数据失败:{}'.format(e))
con.rollback()
finally:
if cursor:
cursor.close()
if con:
con.close()
上周考试错题总结
暂无。
其他
近期学习时间较紧,还需要增加学习消化时间。
学习进度条
| | 代码行数(新增/累积)| 博客量(新增/累积)|学习时间(新增/累积)|重要成长|
| 第一周 | 200/200 | 2/2 | 20/20 | |
| 第二周 | 200/400 | 2/4 | 20/40 |
|
| 第三周 | 200/600 | 2/6 | 20/60 |
|
| 第四周 | 200/800 | 2/8 | 20/80 |
|
| 第五周 | 200/1000 | 2/10 | 20/100 | |
| 第六周 | 200/1200 | 1/11 | 20/120 | |
| 第七周 | 200/1400 | 1/12 | 20/140 | |
| 第八周 | 200/1600 | 1/13 | 20/160 | |
| 第九周 | 200/1800 | 3/16 | 20/180 | |
| 第十周 | 200/2000 | 1/17 | 20/200 | |
参考资料
《计算机科学概论》《看漫画学Python》