实战|教你用Python制作一款带有界面的NBA爬虫小程序
天作之程
共 7493字,需浏览 15分钟
·
2020-11-25 07:40
wxpython
模块构造一个NBA爬虫小软件,演示效果如下构建GUI界面 举例套用爬虫框架
requests
wx
pymysql
pandas
GUI界面设计
wx
库#一、引用模块
import wx
wxpython
模块,建立GUI的模块很多,常见的有PyQt
、Tkinter
等。这些模块各有各的优缺点,读者可以翻阅相关资料进行选择。#二、定义全局变量(创建面板及布局)
class MyFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'titlename',size=(400, 300))
panel = wx.Panel(self)
self.bt_confirm = wx.Button(panel, label='name1')
self.bt_confirm.Bind(wx.EVT_BUTTON,self.OnclickSubmit)
self.bt_cancel = wx.Button(panel, label='name2')
self.bt_cancel.Bind(wx.EVT_BUTTON,self.OnclickCancel)
self.InitUI()
#三、调用局部变量并绑定事件
def InitUI(self):
""" 点击InitUI,执行方法 """
def OnclickSubmit(self,event):
""" 绑定OnclickSubmit事件 """
#四、GUI执行脚本
if __name__ == '__main__':
app = wx.App() # 初始化
frame = MyFrame(parent=None,id=-1)
frame.Show()
app.MainLoop() # 调用主循环
del app
举例实现
class MyFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'NBA可视化',size=(400, 300))
panel = wx.Panel(self)
self.bt_confirm = wx.Button(panel, label='合同信息')
self.bt_confirm.Bind(wx.EVT_BUTTON,self.OnclickSubmit)
self.bt_cancel = wx.Button(panel, label='清空')
self.bt_cancel.Bind(wx.EVT_BUTTON,self.OnclickCancel)
self.bt_imf = wx.Button(panel, label='可视化')
self.bt_imf.Bind(wx.EVT_BUTTON,self.Onclickvisual)
self.bt_team = wx.Button(panel, label='球队信息',pos=(280,20))
self.bt_team.Bind(wx.EVT_BUTTON,self.Onclickteam)
self.bt_obtain = wx.Button(panel, label='球员信息',pos=(20,20))
self.bt_obtain.Bind(wx.EVT_BUTTON,self.Onclickimfor)
self.bt_ml = wx.Button(panel, label='得分榜')
self.bt_ml.Bind(wx.EVT_BUTTON,self.Onclickmql)
self.title = wx.StaticText(panel, label="NBA可视化")
self.label_user = wx.StaticText(panel, label="球队名称")
self.text_user = wx.TextCtrl(panel, style=wx.TE_LEFT)
self.label_pwd = wx.StaticText(panel, label="球员名称")
self.text_pwd = wx.TextCtrl(panel, style=wx.TE_LEFT)
self.label_path = wx.StaticText(panel, label="储存路径")
self.text_pathword = wx.TextCtrl(panel, style=wx.TE_LEFT)
hsizer_user = wx.BoxSizer(wx.HORIZONTAL)
hsizer_user.Add(self.label_user, proportion=0, flag=wx.ALL, border=5)
hsizer_user.Add(self.text_user, proportion=1, flag=wx.ALL, border=5)
hsizer_pwd = wx.BoxSizer(wx.HORIZONTAL)
hsizer_pwd.Add(self.label_pwd, proportion=0, flag=wx.ALL, border=5)
hsizer_pwd.Add(self.text_pwd, proportion=1, flag=wx.ALL, border=5)
hsizer_path = wx.BoxSizer(wx.HORIZONTAL)
hsizer_path.Add(self.label_path, proportion=0, flag=wx.ALL, border=5)
hsizer_path.Add(self.text_pathword, proportion=1, flag=wx.ALL, border=5)
hsizer_button = wx.BoxSizer(wx.HORIZONTAL)
hsizer_button.Add(self.bt_confirm, proportion=0, flag=wx.ALIGN_CENTER, border=5)
hsizer_button.Add(self.bt_cancel, proportion=0, flag=wx.ALIGN_CENTER, border=5)
hsizer_button.Add(self.bt_imf, proportion=0, flag=wx.ALIGN_CENTER, border=5)
hsizer_button.Add(self.bt_ml, proportion=0, flag=wx.ALIGN_CENTER, border=5)
vsizer_all = wx.BoxSizer(wx.VERTICAL)
vsizer_all.Add(self.title, proportion=0, flag=wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER,
border=15)
vsizer_all.Add(hsizer_user, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=45)
vsizer_all.Add(hsizer_pwd, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=45)
vsizer_all.Add(hsizer_path, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=45)
vsizer_all.Add(hsizer_button, proportion=0, flag=wx.ALIGN_CENTER | wx.TOP, border=15)
panel.SetSizer(vsizer_all)
self.InitUI()
def InitUI(self):
""" 点击使用说明按钮,执行方法 """
def OnQuit1(self,e):
""" 输入注意事项 """
def OnclickSubmit(self,event):
""" 点击合同信息按钮,执行方法 """
def Onclickvisual(self,event):
""" 点击可视化按钮,执行方法 """
def OnclickCancel(self,event):
""" 点击清空按钮,执行方法 """
def Onclickimfor(self,event):
""" 点击球员名称按钮,执行方法 """
def Onclickteam(self,event):
""" 点击球队名称按钮,执行方法 """
def Onclickmql(self,event):
""" 点击得分榜按钮,执行方法 """
if __name__ == '__main__':
app = wx.App() # 初始化
frame = MyFrame(parent=None,id=-1) # 实例MyFrame类,并传递参数
frame.Show() # 显示窗口
app.MainLoop() # 调用主循环方法
补充:插入背景图片
base64
化,转换后存入py文件后以import为媒介才能打包。二进制代码转换如下:import base64
with open("name.jpg","rb") as f:
base64_str = base64.b64encode(f.read())
with open('%s.py' % picture_name.replace('.', '_'), 'w+') as f1:
f1.write(base64_str)
f1.close()
base64
编码的py文件,而后在代码中进行引用。由于打包不能打包图片,故这里稍微复杂的实现“引用指定图片的base64
编码——创建图片——插入背景图片”功能!#这里需要在主事件中插入两句话
panel.Bind(wx.EVT_ERASE_BACKGROUND,self.OnEraseBack)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBack)
#引用编码并创建图片
from bg_png import img as bg
def pic(picPath,picName):
tmp = open(picPath, 'wb')
tmp.write(base64.b64decode(picName))
tmp.close()
pic('bg.png',bg)
#插入图片(子事件 有缩进)
def OnEraseBack(self,event):
'''加入图片背景'''
try :
dc = event.GetDC()
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
bmp = wx.Bitmap(nowpath+r'\bg.png')
dc.DrawBitmap(bmp, -500, -100)
except :
pass
#将文本底色改为透明
#第一步:将主事件中wx.StaticText全部换成TransparentStaticText
#第二步:重现StaticText控件
class TransparentStaticText(wx.StaticText):
def __init__(self, parent, id=wx.ID_ANY, label='', pos=wx.DefaultPosition, size=wx.DefaultSize,
style=wx.TRANSPARENT_WINDOW, name='TransparentStaticText'):
wx.StaticText.__init__(self, parent, id, label, pos, size, style, name)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, lambda event: None)
self.Bind(wx.EVT_SIZE, self.OnSize)
def OnPaint(self, event):
bdc = wx.PaintDC(self)
dc = wx.GCDC(bdc)
font_face = self.GetFont()
font_color = self.GetForegroundColour()
dc.SetFont(font_face)
dc.SetTextForeground(font_color)
dc.DrawText(self.GetLabel(), 0, 0)
def OnSize(self, event):
self.Refresh()
event.Skip()
import six
import packaging
import packaging.version
import packaging.specifiers
import packaging.requirements
-END- 往期精彩推荐 -- -- 1、在线代码编辑器,可以分享给任何人 -- 2、Python 造假数据,用Faker就够了 -- 3、在Python中玩转Json数据 -- 留下你的“在看”呗!
评论