更新提醒:

python大全写的内容比较深,所以可能时间要久一点。

大家好,我又来了。今天我要为大家分享一下那些有用的包


目录

一、怎么下载包

二、了解一下包

三、示例

四、总结


一、怎么下载包

首先,我们先要pip install 包,具体方法如下:

打开cmd(命令提示符),输入以下内容:pip install pygame

这意味着找不到这个包

成功安装

——示例电脑版本为微软windows11,因为版本不同,软件不同,所以内容可能不一样

这只是示例,想要判断包有没有成功下载,我们来看一看!

成功下载(以前下载过)

pip list里面把所有安装过的包都显示出来了

小提示:

在pip的时候有一种方法是这样的:

pip3.9 install pygame

也就是pip版本 install包名

二、了解一下包

按照上面的路径可以看见python里面所有的包

列举一部分的包,py文件就是单独的包,文件夹里面的都是很重要、复杂的包

site-packages里面的包都是十分重要的,有很多版本的包

三、示例

我们来看一看tkinter的包,里面是什么样的?

里面分为pycache文件夹,__init__.py文件和其他py文件
"""Wrapper functions for Tcl/Tk.Tkinter provides classes which allow the display, positioning and
control of widgets. Toplevel widgets are Tk and Toplevel. Other
widgets are Frame, Label, Entry, Text, Canvas, Button, Radiobutton,
Checkbutton, Scale, Listbox, Scrollbar, OptionMenu, Spinbox
LabelFrame and PanedWindow.Properties of the widgets are specified with keyword arguments.
Keyword arguments have the same name as the corresponding resource
under Tk.Widgets are positioned with one of the geometry managers Place, Pack
or Grid. These managers can be called with methods place, pack, grid
available in every Widget.Actions are bound to events by resources (e.g. keyword argument
command) or with the method bind.Example (Hello, World):
import tkinter
from tkinter.constants import *
tk = tkinter.Tk()
frame = tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
frame.pack(fill=BOTH,expand=1)
label = tkinter.Label(frame, text="Hello, World")
label.pack(fill=X, expand=1)
button = tkinter.Button(frame,text="Exit",command=tk.destroy)
button.pack(side=BOTTOM)
tk.mainloop()
"""import enum
import sys
import typesimport _tkinter # If this fails your Python may not be configured for Tk
TclError = _tkinter.TclError
from tkinter.constants import *
import rewantobjects = 1TkVersion = float(_tkinter.TK_VERSION)
TclVersion = float(_tkinter.TCL_VERSION)READABLE = _tkinter.READABLE
WRITABLE = _tkinter.WRITABLE
EXCEPTION = _tkinter.EXCEPTION_magic_re = re.compile(r'([\\{}])')
_space_re = re.compile(r'([\s])', re.ASCII)def _join(value):"""Internal function."""return ' '.join(map(_stringify, value))def _stringify(value):"""Internal function."""if isinstance(value, (list, tuple)):if len(value) == 1:value = _stringify(value[0])if _magic_re.search(value):value = '{%s}' % valueelse:value = '{%s}' % _join(value)else:value = str(value)if not value:value = '{}'elif _magic_re.search(value):# add '\' before special characters and spacesvalue = _magic_re.sub(r'\\\1', value)value = value.replace('\n', r'\n')value = _space_re.sub(r'\\\1', value)if value[0] == '"':value = '\\' + valueelif value[0] == '"' or _space_re.search(value):value = '{%s}' % valuereturn valuedef _flatten(seq):"""Internal function."""res = ()for item in seq:if isinstance(item, (tuple, list)):res = res + _flatten(item)elif item is not None:res = res + (item,)return restry: _flatten = _tkinter._flatten
except AttributeError: passdef _cnfmerge(cnfs):"""Internal function."""if isinstance(cnfs, dict):return cnfselif isinstance(cnfs, (type(None), str)):return cnfselse:cnf = {}for c in _flatten(cnfs):try:cnf.update(c)except (AttributeError, TypeError) as msg:print("_cnfmerge: fallback due to:", msg)for k, v in c.items():cnf[k] = vreturn cnftry: _cnfmerge = _tkinter._cnfmerge
except AttributeError: passdef _splitdict(tk, v, cut_minus=True, conv=None):"""Return a properly formatted dict built from Tcl list pairs.If cut_minus is True, the supposed '-' prefix will be removed fromkeys. If conv is specified, it is used to convert values.Tcl list is expected to contain an even number of elements."""t = tk.splitlist(v)if len(t) % 2:raise RuntimeError('Tcl list representing a dict is expected ''to contain an even number of elements')it = iter(t)dict = {}for key, value in zip(it, it):key = str(key)if cut_minus and key[0] == '-':key = key[1:]if conv:value = conv(value)dict[key] = valuereturn dictclass EventType(str, enum.Enum):KeyPress = '2'Key = KeyPressKeyRelease = '3'ButtonPress = '4'Button = ButtonPressButtonRelease = '5'Motion = '6'Enter = '7'Leave = '8'FocusIn = '9'FocusOut = '10'Keymap = '11'           # undocumentedExpose = '12'GraphicsExpose = '13'   # undocumentedNoExpose = '14'         # undocumentedVisibility = '15'Create = '16'Destroy = '17'Unmap = '18'Map = '19'MapRequest = '20'Reparent = '21'Configure = '22'ConfigureRequest = '23'Gravity = '24'ResizeRequest = '25'Circulate = '26'CirculateRequest = '27'Property = '28'SelectionClear = '29'   # undocumentedSelectionRequest = '30' # undocumentedSelection = '31'        # undocumentedColormap = '32'ClientMessage = '33'    # undocumentedMapping = '34'          # undocumentedVirtualEvent = '35'     # undocumentedActivate = '36'Deactivate = '37'MouseWheel = '38'__str__ = str.__str__class Event:"""Container for the properties of an event.Instances of this type are generated if one of the following events occurs:KeyPress, KeyRelease - for keyboard eventsButtonPress, ButtonRelease, Motion, Enter, Leave, MouseWheel - for mouse eventsVisibility, Unmap, Map, Expose, FocusIn, FocusOut, Circulate,Colormap, Gravity, Reparent, Property, Destroy, Activate,Deactivate - for window events.If a callback function for one of these events is registeredusing bind, bind_all, bind_class, or tag_bind, the callback iscalled with an Event as first argument. It will have thefollowing attributes (in braces are the event types for whichthe attribute is valid):serial - serial number of eventnum - mouse button pressed (ButtonPress, ButtonRelease)focus - whether the window has the focus (Enter, Leave)height - height of the exposed window (Configure, Expose)width - width of the exposed window (Configure, Expose)keycode - keycode of the pressed key (KeyPress, KeyRelease)state - state of the event as a number (ButtonPress, ButtonRelease,Enter, KeyPress, KeyRelease,Leave, Motion)state - state as a string (Visibility)time - when the event occurredx - x-position of the mousey - y-position of the mousex_root - x-position of the mouse on the screen(ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion)y_root - y-position of the mouse on the screen(ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion)char - pressed character (KeyPress, KeyRelease)send_event - see X/Windows documentationkeysym - keysym of the event as a string (KeyPress, KeyRelease)keysym_num - keysym of the event as a number (KeyPress, KeyRelease)type - type of the event as a numberwidget - widget in which the event occurreddelta - delta of wheel movement (MouseWheel)"""def __repr__(self):attrs = {k: v for k, v in self.__dict__.items() if v != '??'}if not self.char:del attrs['char']elif self.char != '??':attrs['char'] = repr(self.char)if not getattr(self, 'send_event', True):del attrs['send_event']if self.state == 0:del attrs['state']elif isinstance(self.state, int):state = self.statemods = ('Shift', 'Lock', 'Control','Mod1', 'Mod2', 'Mod3', 'Mod4', 'Mod5','Button1', 'Button2', 'Button3', 'Button4', 'Button5')s = []for i, n in enumerate(mods):if state & (1 << i):s.append(n)state = state & ~((1<< len(mods)) - 1)if state or not s:s.append(hex(state))attrs['state'] = '|'.join(s)if self.delta == 0:del attrs['delta']# widget usually is known# serial and time are not very interesting# keysym_num duplicates keysym# x_root and y_root mostly duplicate x and ykeys = ('send_event','state', 'keysym', 'keycode', 'char','num', 'delta', 'focus','x', 'y', 'width', 'height')return '<%s event%s>' % (getattr(self.type, 'name', self.type),''.join(' %s=%s' % (k, attrs[k]) for k in keys if k in attrs))_support_default_root = True
_default_root = Nonedef NoDefaultRoot():"""Inhibit setting of default root window.Call this function to inhibit that the first instance ofTk is used for windows without an explicit parent window."""global _support_default_root, _default_root_support_default_root = False# Delete, so any use of _default_root will immediately raise an exception.# Rebind before deletion, so repeated calls will not fail._default_root = Nonedel _default_rootdef _get_default_root(what=None):if not _support_default_root:raise RuntimeError("No master specified and tkinter is ""configured to not support default root")if _default_root is None:if what:raise RuntimeError(f"Too early to {what}: no default root window")root = Tk()assert _default_root is rootreturn _default_rootdef _get_temp_root():global _support_default_rootif not _support_default_root:raise RuntimeError("No master specified and tkinter is ""configured to not support default root")root = _default_rootif root is None:assert _support_default_root_support_default_root = Falseroot = Tk()_support_default_root = Trueassert _default_root is Noneroot.withdraw()root._temporary = Truereturn rootdef _destroy_temp_root(master):if getattr(master, '_temporary', False):try:master.destroy()except TclError:passdef _tkerror(err):"""Internal function."""passdef _exit(code=0):"""Internal function. Calling it will raise the exception SystemExit."""try:code = int(code)except ValueError:passraise SystemExit(code)_varnum = 0class Variable:"""Class to define value holders for e.g. buttons.Subclasses StringVar, IntVar, DoubleVar, BooleanVar are specializationsthat constrain the type of the value returned from get()."""_default = ""_tk = None_tclCommands = Nonedef __init__(self, master=None, value=None, name=None):"""Construct a variableMASTER can be given as master widget.VALUE is an optional value (defaults to "")NAME is an optional Tcl name (defaults to PY_VARnum).If NAME matches an existing variable and VALUE is omittedthen the existing value is retained."""# check for type of NAME parameter to override weird error message# raised from Modules/_tkinter.c:SetVar like:# TypeError: setvar() takes exactly 3 arguments (2 given)if name is not None and not isinstance(name, str):raise TypeError("name must be a string")global _varnumif master is None:master = _get_default_root('create variable')self._root = master._root()self._tk = master.tkif name:self._name = nameelse:self._name = 'PY_VAR' + repr(_varnum)_varnum += 1if value is not None:self.initialize(value)elif not self._tk.getboolean(self._tk.call("info", "exists", self._name)):self.initialize(self._default)def __del__(self):"""Unset the variable in Tcl."""if self._tk is None:returnif self._tk.getboolean(self._tk.call("info", "exists", self._name)):self._tk.globalunsetvar(self._name)if self._tclCommands is not None:for name in self._tclCommands:#print '- Tkinter: deleted command', nameself._tk.deletecommand(name)self._tclCommands = Nonedef __str__(self):"""Return the name of the variable in Tcl."""return self._namedef set(self, value):"""Set the variable to VALUE."""return self._tk.globalsetvar(self._name, value)initialize = setdef get(self):"""Return value of variable."""return self._tk.globalgetvar(self._name)def _register(self, callback):f = CallWrapper(callback, None, self._root).__call__cbname = repr(id(f))try:callback = callback.__func__except AttributeError:passtry:cbname = cbname + callback.__name__except AttributeError:passself._tk.createcommand(cbname, f)if self._tclCommands is None:self._tclCommands = []self._tclCommands.append(cbname)return cbnamedef trace_add(self, mode, callback):"""Define a trace callback for the variable.Mode is one of "read", "write", "unset", or a list or tuple ofsuch strings.Callback must be a function which is called when the variable isread, written or unset.Return the name of the callback."""cbname = self._register(callback)self._tk.call('trace', 'add', 'variable',self._name, mode, (cbname,))return cbnamedef trace_remove(self, mode, cbname):"""Delete the trace callback for a variable.Mode is one of "read", "write", "unset" or a list or tuple ofsuch strings.  Must be same as were specified in trace_add().cbname is the name of the callback returned from trace_add()."""self._tk.call('trace', 'remove', 'variable',self._name, mode, cbname)for m, ca in self.trace_info():if self._tk.splitlist(ca)[0] == cbname:breakelse:self._tk.deletecommand(cbname)try:self._tclCommands.remove(cbname)except ValueError:passdef trace_info(self):"""Return all trace callback information."""splitlist = self._tk.splitlistreturn [(splitlist(k), v) for k, v in map(splitlist,splitlist(self._tk.call('trace', 'info', 'variable', self._name)))]def trace_variable(self, mode, callback):"""Define a trace callback for the variable.MODE is one of "r", "w", "u" for read, write, undefine.CALLBACK must be a function which is called whenthe variable is read, written or undefined.Return the name of the callback.This deprecated method wraps a deprecated Tcl method that willlikely be removed in the future.  Use trace_add() instead."""# TODO: Add deprecation warningcbname = self._register(callback)self._tk.call("trace", "variable", self._name, mode, cbname)return cbnametrace = trace_variabledef trace_vdelete(self, mode, cbname):"""Delete the trace callback for a variable.MODE is one of "r", "w", "u" for read, write, undefine.CBNAME is the name of the callback returned from trace_variable or trace.This deprecated method wraps a deprecated Tcl method that willlikely be removed in the future.  Use trace_remove() instead."""# TODO: Add deprecation warningself._tk.call("trace", "vdelete", self._name, mode, cbname)cbname = self._tk.splitlist(cbname)[0]for m, ca in self.trace_info():if self._tk.splitlist(ca)[0] == cbname:breakelse:self._tk.deletecommand(cbname)try:self._tclCommands.remove(cbname)except ValueError:passdef trace_vinfo(self):"""Return all trace callback information.This deprecated method wraps a deprecated Tcl method that willlikely be removed in the future.  Use trace_info() instead."""# TODO: Add deprecation warningreturn [self._tk.splitlist(x) for x in self._tk.splitlist(self._tk.call("trace", "vinfo", self._name))]def __eq__(self, other):if not isinstance(other, Variable):return NotImplementedreturn (self._name == other._nameand self.__class__.__name__ == other.__class__.__name__and self._tk == other._tk)

因为代码太长了,所以我就列举一部分内容。

大家是不是都有一个问题:为什么包里面都只有函数和类呢?

我们来看一看我们在用这个包的时候用的语句:

from tkinter import *
# ttk覆盖tkinter部分对象,ttk对tkinter进行了优化
from tkinter.ttk import *
# 深拷贝时需要用到copy模块
import copy
import tkinter.messagebox
# 围棋应用对象定义
class Application(Tk):# 初始化棋盘,默认九路棋盘def __init__(self,my_mode_num=9):Tk.__init__(self)# 模式,九路棋:9,十三路棋:13,十九路棋:19self.mode_num=my_mode_num# 窗口尺寸设置,默认:1.8self.size=1.8# 棋盘每格的边长self.dd=360*self.size/(self.mode_num-1)# 相对九路棋盘的矫正比例self.p=1 if self.mode_num==9 else (2/3 if self.mode_num==13 else 4/9)# 定义棋盘阵列,超过边界:-1,无子:0,黑棋:1,白棋:2self.positions=[[0 for i in range(self.mode_num+2)] for i in range(self.mode_num+2)]# 初始化棋盘,所有超过边界的值置-1for m in range(self.mode_num+2):for n in range(self.mode_num+2):if (m*n==0 or m==self.mode_num+1 or n==self.mode_num+1):self.positions[m][n]=-1# 拷贝三份棋盘“快照”,悔棋和判断“打劫”时需要作参考self.last_3_positions=copy.deepcopy(self.positions)self.last_2_positions=copy.deepcopy(self.positions)self.last_1_positions=copy.deepcopy(self.positions)# 记录鼠标经过的地方,用于显示shadow时self.cross_last=None# 当前轮到的玩家,黑:0,白:1,执黑先行self.present=0 # 初始停止运行,点击“开始游戏”运行游戏self.stop=True# 悔棋次数,次数大于0才可悔棋,初始置0(初始不能悔棋),悔棋后置0,下棋或弃手时恢复为1,以禁止连续悔棋self.regretchance=0# 图片资源,存放在当前目录下的/Pictures/中self.photoW=PhotoImage(file = "./Pictures/W.png")self.photoB=PhotoImage(file = "./Pictures/B.png")self.photoBD=PhotoImage(file = "./Pictures/"+"BD"+"-"+str(self.mode_num)+".png")self.photoWD=PhotoImage(file = "./Pictures/"+"WD"+"-"+str(self.mode_num)+".png")self.photoBU=PhotoImage(file = "./Pictures/"+"BU"+"-"+str(self.mode_num)+".png")self.photoWU=PhotoImage(file = "./Pictures/"+"WU"+"-"+str(self.mode_num)+".png")# 用于黑白棋子图片切换的列表self.photoWBU_list=[self.photoBU,self.photoWU]self.photoWBD_list=[self.photoBD,self.photoWD]# 窗口大小self.geometry(str(int(600*self.size))+'x'+str(int(400*self.size)))# 画布控件,作为容器self.canvas_bottom=Canvas(self,bg='#369',bd=0,width=600*self.size,height=400*self.size)self.canvas_bottom.place(x=0,y=0)# 几个功能按钮self.startButton=Button(self,text='开始游戏',command=self.start)self.startButton.place(x=480*self.size,y=200*self.size)self.passmeButton=Button(self,text='弃一手',command=self.passme)self.passmeButton.place(x=480*self.size,y=225*self.size)  self.regretButton=Button(self,text='悔棋',command=self.regret)self.regretButton.place(x=480*self.size,y=250*self.size)# 初始悔棋按钮禁用self.regretButton['state']=DISABLEDself.replayButton=Button(self,text='重新开始',command=self.reload)self.replayButton.place(x=480*self.size,y=275*self.size)self.newGameButton1=Button(self,text=('十三' if self.mode_num==9 else '九')+'路棋',command=self.newGame1)self.newGameButton1.place(x=480*self.size,y=300*self.size)self.newGameButton2=Button(self,text=('十三' if self.mode_num==19 else '十九')+'路棋',command=self.newGame2)self.newGameButton2.place(x=480*self.size,y=325*self.size)self.quitButton=Button(self,text='退出游戏',command=self.quit)self.quitButton.place(x=480*self.size,y=350*self.size)# 画棋盘,填充颜色self.canvas_bottom.create_rectangle(0*self.size,0*self.size,400*self.size,400*self.size,fill='#c51')# 刻画棋盘线及九个点# 先画外框粗线self.canvas_bottom.create_rectangle(20*self.size,20*self.size,380*self.size,380*self.size,width=3)# 棋盘上的九个定位点,以中点为模型,移动位置,以作出其余八个点for m in [-1,0,1]:for n in [-1,0,1]:self.oringinal=self.canvas_bottom.create_oval(200*self.size-self.size*2,200*self.size-self.size*2,200*self.size+self.size*2,200*self.size+self.size*2,fill='#000')self.canvas_bottom.move(self.oringinal,m*self.dd*(2 if self.mode_num==9 else (3 if self.mode_num==13 else 6)),n*self.dd*(2 if self.mode_num==9 else (3 if self.mode_num==13 else 6)))# 画中间的线条for i in range(1,self.mode_num-1):self.canvas_bottom.create_line(20*self.size,20*self.size+i*self.dd,380*self.size,20*self.size+i*self.dd,width=2)self.canvas_bottom.create_line(20*self.size+i*self.dd,20*self.size,20*self.size+i*self.dd,380*self.size,width=2)# 放置右侧初始图片self.pW=self.canvas_bottom.create_image(500*self.size+11, 65*self.size,image=self.photoW)self.pB=self.canvas_bottom.create_image(500*self.size-11, 65*self.size,image=self.photoB)# 每张图片都添加image标签,方便reload函数删除图片self.canvas_bottom.addtag_withtag('image',self.pW)self.canvas_bottom.addtag_withtag('image',self.pB)# 鼠标移动时,调用shadow函数,显示随鼠标移动的棋子self.canvas_bottom.bind('<Motion>',self.shadow)# 鼠标左键单击时,调用getdown函数,放下棋子self.canvas_bottom.bind('<Button-1>',self.getDown)# 设置退出快捷键<Ctrl>+<D>,快速退出游戏self.bind('<Control-KeyPress-d>',self.keyboardQuit)# 开始游戏函数,点击“开始游戏”时调用def start(self):# 删除右侧太极图self.canvas_bottom.delete(self.pW)self.canvas_bottom.delete(self.pB)# 利用右侧图案提示开始时谁先落子if self.present==0:self.create_pB()self.del_pW()else:self.create_pW()self.del_pB()# 开始标志,解除stopself.stop=None# 放弃一手函数,跳过落子环节def passme(self):# 悔棋恢复if not self.regretchance==1:self.regretchance+=1else:self.regretButton['state']=NORMAL# 拷贝棋盘状态,记录前三次棋局self.last_3_positions=copy.deepcopy(self.last_2_positions)self.last_2_positions=copy.deepcopy(self.last_1_positions)self.last_1_positions=copy.deepcopy(self.positions)self.canvas_bottom.delete('image_added_sign')# 轮到下一玩家if self.present==0:self.create_pW()self.del_pB()self.present=1else:self.create_pB()self.del_pW()self.present=0# 悔棋函数,可悔棋一回合,下两回合不可悔棋def regret(self):# 判定是否可以悔棋,以前第三盘棋局复原棋盘if self.regretchance==1:self.regretchance=0self.regretButton['state']=DISABLEDlist_of_b=[]list_of_w=[]self.canvas_bottom.delete('image')if self.present==0:self.create_pB()else:self.create_pW()for m in range(1,self.mode_num+1):for n in range(1,self.mode_num+1):self.positions[m][n]=0for m in range(len(self.last_3_positions)):for n in range(len(self.last_3_positions[m])):if self.last_3_positions[m][n]==1:list_of_b+=[[n,m]]elif self.last_3_positions[m][n]==2:list_of_w+=[[n,m]]self.recover(list_of_b,0)self.recover(list_of_w,1)self.last_1_positions=copy.deepcopy(self.last_3_positions)for m in range(1,self.mode_num+1):for n in range(1,self.mode_num+1):self.last_2_positions[m][n]=0self.last_3_positions[m][n]=0# 重新加载函数,删除图片,序列归零,设置一些初始参数,点击“重新开始”时调用def reload(self):if self.stop==1:self.stop=0self.canvas_bottom.delete('image')self.regretchance=0self.present=0self.create_pB()for m in range(1,self.mode_num+1):for n in range(1,self.mode_num+1):self.positions[m][n]=0self.last_3_positions[m][n]=0self.last_2_positions[m][n]=0self.last_1_positions[m][n]=0# 以下四个函数实现了右侧太极图的动态创建与删除def create_pW(self):self.pW=self.canvas_bottom.create_image(500*self.size+11, 65*self.size,image=self.photoW)self.canvas_bottom.addtag_withtag('image',self.pW)def create_pB(self):self.pB=self.canvas_bottom.create_image(500*self.size-11, 65*self.size,image=self.photoB)self.canvas_bottom.addtag_withtag('image',self.pB)def del_pW(self):self.canvas_bottom.delete(self.pW)def del_pB(self):self.canvas_bottom.delete(self.pB)# 显示鼠标移动下棋子的移动def shadow(self,event):if not self.stop:# 找到最近格点,在当前位置靠近的格点出显示棋子图片,并删除上一位置的棋子图片if (20*self.size<event.x<380*self.size) and (20*self.size<event.y<380*self.size):dx=(event.x-20*self.size)%self.dddy=(event.y-20*self.size)%self.ddself.cross=self.canvas_bottom.create_image(event.x-dx+round(dx/self.dd)*self.dd+22*self.p, event.y-dy+round(dy/self.dd)*self.dd-27*self.p,image=self.photoWBU_list[self.present])self.canvas_bottom.addtag_withtag('image',self.cross)if self.cross_last!=None:self.canvas_bottom.delete(self.cross_last)self.cross_last=self.cross# 落子,并驱动玩家的轮流下棋行为def getDown(self,event):if not self.stop:# 先找到最近格点if (20*self.size-self.dd*0.4<event.x<self.dd*0.4+380*self.size) and (20*self.size-self.dd*0.4<event.y<self.dd*0.4+380*self.size):dx=(event.x-20*self.size)%self.dddy=(event.y-20*self.size)%self.ddx=int((event.x-20*self.size-dx)/self.dd+round(dx/self.dd)+1)y=int((event.y-20*self.size-dy)/self.dd+round(dy/self.dd)+1)# 判断位置是否已经被占据if self.positions[y][x]==0:# 未被占据,则尝试占据,获得占据后能杀死的棋子列表self.positions[y][x]=self.present+1self.image_added=self.canvas_bottom.create_image(event.x-dx+round(dx/self.dd)*self.dd+4*self.p, event.y-dy+round(dy/self.dd)*self.dd-5*self.p,image=self.photoWBD_list[self.present])self.canvas_bottom.addtag_withtag('image',self.image_added)# 棋子与位置标签绑定,方便“杀死”self.canvas_bottom.addtag_withtag('position'+str(x)+str(y),self.image_added)deadlist=self.get_deadlist(x,y)self.kill(deadlist)# 判断是否重复棋局if not self.last_2_positions==self.positions:# 判断是否属于有气和杀死对方其中之一if len(deadlist)>0 or self.if_dead([[x,y]],self.present+1,[x,y])==False:# 当不重复棋局,且属于有气和杀死对方其中之一时,落下棋子有效if not self.regretchance==1:self.regretchance+=1else:self.regretButton['state']=NORMALself.last_3_positions=copy.deepcopy(self.last_2_positions)self.last_2_positions=copy.deepcopy(self.last_1_positions)self.last_1_positions=copy.deepcopy(self.positions)# 删除上次的标记,重新创建标记self.canvas_bottom.delete('image_added_sign')self.image_added_sign=self.canvas_bottom.create_oval(event.x-dx+round(dx/self.dd)*self.dd+0.5*self.dd, event.y-dy+round(dy/self.dd)*self.dd+0.5*self.dd,event.x-dx+round(dx/self.dd)*self.dd-0.5*self.dd, event.y-dy+round(dy/self.dd)*self.dd-0.5*self.dd,width=3,outline='#3ae')self.canvas_bottom.addtag_withtag('image',self.image_added_sign)self.canvas_bottom.addtag_withtag('image_added_sign',self.image_added_sign)if self.present==0:self.create_pW()self.del_pB()self.present=1else:self.create_pB()self.del_pW()self.present=0else:# 不属于杀死对方或有气,则判断为无气,警告并弹出警告框self.positions[y][x]=0self.canvas_bottom.delete('position'+str(x)+str(y))self.bell()self.showwarningbox('无气',"你被包围了!")else:# 重复棋局,警告打劫self.positions[y][x]=0self.canvas_bottom.delete('position'+str(x)+str(y))self.recover(deadlist,(1 if self.present==0 else 0))self.bell()self.showwarningbox("打劫","此路不通!")else:# 覆盖,声音警告self.bell()else:# 超出边界,声音警告self.bell()# 判断棋子(种类为yourChessman,位置为yourPosition)是否无气(死亡),有气则返回False,无气则返回无气棋子的列表# 本函数是游戏规则的关键,初始deadlist只包含了自己的位置,每次执行时,函数尝试寻找yourPosition周围有没有空的位置,有则结束,返回False代表有气;# 若找不到,则找自己四周的同类(不在deadlist中的)是否有气,即调用本函数,无气,则把该同类加入到deadlist,然后找下一个邻居,只要有一个有气,返回False代表有气;# 若四周没有一个有气的同类,返回deadlist,至此结束递归# def if_dead(self,deadlist,yourChessman,yourPosition):def if_dead(self,deadList,yourChessman,yourPosition):for i in [-1,1]:if [yourPosition[0]+i,yourPosition[1]] not in deadList:if self.positions[yourPosition[1]][yourPosition[0]+i]==0:return Falseif [yourPosition[0],yourPosition[1]+i] not in deadList:if self.positions[yourPosition[1]+i][yourPosition[0]]==0:return Falseif ([yourPosition[0]+1,yourPosition[1]] not in deadList) and (self.positions[yourPosition[1]][yourPosition[0]+1]==yourChessman):midvar=self.if_dead(deadList+[[yourPosition[0]+1,yourPosition[1]]],yourChessman,[yourPosition[0]+1,yourPosition[1]])if not midvar:return Falseelse:deadList+=copy.deepcopy(midvar)if ([yourPosition[0]-1,yourPosition[1]] not in deadList) and (self.positions[yourPosition[1]][yourPosition[0]-1]==yourChessman):midvar=self.if_dead(deadList+[[yourPosition[0]-1,yourPosition[1]]],yourChessman,[yourPosition[0]-1,yourPosition[1]])if not midvar:return Falseelse:deadList+=copy.deepcopy(midvar)if ([yourPosition[0],yourPosition[1]+1] not in deadList) and (self.positions[yourPosition[1]+1][yourPosition[0]]==yourChessman):midvar=self.if_dead(deadList+[[yourPosition[0],yourPosition[1]+1]],yourChessman,[yourPosition[0],yourPosition[1]+1])if not midvar:return Falseelse:deadList+=copy.deepcopy(midvar)if ([yourPosition[0],yourPosition[1]-1] not in deadList) and (self.positions[yourPosition[1]-1][yourPosition[0]]==yourChessman):midvar=self.if_dead(deadList+[[yourPosition[0],yourPosition[1]-1]],yourChessman,[yourPosition[0],yourPosition[1]-1])if not midvar:return Falseelse:deadList+=copy.deepcopy(midvar)return deadList  # 警告消息框,接受标题和警告信息            def showwarningbox(self,title,message):self.canvas_bottom.delete(self.cross)tkinter.messagebox.showwarning(title,message)# 落子后,依次判断四周是否有棋子被杀死,并返回死棋位置列表def get_deadlist(self,x,y):deadlist=[]for i in [-1,1]:if self.positions[y][x+i]==(2 if self.present==0 else 1) and ([x+i,y] not in deadlist):killList=self.if_dead([[x+i,y]],(2 if self.present==0 else 1),[x+i,y])if not killList==False:deadlist+=copy.deepcopy(killList)if self.positions[y+i][x]==(2 if self.present==0 else 1) and ([x,y+i] not in deadlist):       killList=self.if_dead([[x,y+i]],(2 if self.present==0 else 1),[x,y+i])if not killList==False:deadlist+=copy.deepcopy(killList)return deadlist# 恢复位置列表list_to_recover为b_or_w指定的棋子def recover(self,list_to_recover,b_or_w):if len(list_to_recover)>0:for i in range(len(list_to_recover)):self.positions[list_to_recover[i][1]][list_to_recover[i][0]]=b_or_w+1self.image_added=self.canvas_bottom.create_image(20*self.size+(list_to_recover[i][0]-1)*self.dd+4*self.p, 20*self.size+(list_to_recover[i][1]-1)*self.dd-5*self.p,image=self.photoWBD_list[b_or_w])self.canvas_bottom.addtag_withtag('image',self.image_added)self.canvas_bottom.addtag_withtag('position'+str(list_to_recover[i][0])+str(list_to_recover[i][1]),self.image_added)# 杀死位置列表killList中的棋子,即删除图片,位置值置0def kill(self,killList):if len(killList)>0:for i in range(len(killList)):self.positions[killList[i][1]][killList[i][0]]=0self.canvas_bottom.delete('position'+str(killList[i][0])+str(killList[i][1]))# 键盘快捷键退出游戏def keyboardQuit(self,event):self.quit()# 以下两个函数修改全局变量值,newApp使主函数循环,以建立不同参数的对象def newGame1(self):global mode_num,newAppmode_num=(13 if self.mode_num==9 else 9)newApp=Trueself.quit()def newGame2(self):global mode_num,newAppmode_num=(13 if self.mode_num==19 else 19)newApp=Trueself.quit()# 声明全局变量,用于新建Application对象时切换成不同模式的游戏
global mode_num,newApp
mode_num=9
newApp=False
if __name__=='__main__':# 循环,直到不切换游戏模式while True:newApp=Falseapp=Application(mode_num)app.title('围棋')app.mainloop()if newApp:app.destroy()else:break

上面的都是围棋程序。

我们基本都是用包名.函数或者函数

我来举个例子:

import modfrom mod import *from mod.system import *from mod.system import Print

以上包是我自己写的,没有上传到pypi,所以大家不要去pip install mod了,即使你pip到了,但那个也不是我的。

四、总结

这就是包的用法,pip的方法。希望大家可以关注一下,点个赞。

python笔记:模块

里面有关于模块的其他知识。大家可以回忆一下!

python大全-那些有用的包相关推荐

  1. 【涵子来信python大全】——第二季——opencv第四篇-用手势控制屏幕鼠标

    各位亲爱的读者,博主: 大家好,我是涵子.今天我们需要使用cv2,mediapipe和pyautogui用手势控制屏幕鼠标. 目录 一.准备 二.代码 可以先去看看之前的文章哦. 一.准备 首先pip ...

  2. python恶搞表情包下载大全_用 Python 把你的朋友变成表情包

    标签:rgb   get   pytho   尺寸   imp   像素   lan   href   不可 实现步骤 导入朋友的照片(前景照片); 处理前景照片(缩放.旋转,填充); 导入熊猫头照片 ...

  3. python之路——模块和包

    一.模块 1.什么是模块? 常见的场景:一个模块就是一个包含了Python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1.使用Python编 ...

  4. Linux下的Ubuntu16.04系统配置并使用USB转串口(串口转USB),最终使用python的serial和pyserial包实现串口的打开并读取数据

    1. USB转串口的配置 1.1 首先使用 lsmod | grep usbserial 指令查看系统是否包含USB转串口驱动,如果没有信息输出不代表没有驱动,我这边就是没有信息输出,且看后面分析: ...

  5. Python一键保存千张表情包图

    表情包这个东西,现在每个人聊天都会看到.有时候自己发完文字后,不配一个表情包都会觉得很不习惯.不止是在聊天,就比如我现在发文章都要配几个表情包. 跟刚认识的朋友在聊天时,是不是的发几个表情包,都感觉能 ...

  6. 小孩学python有意义吗-小孩Python编程培训有用吗

    小孩Python编程培训有用吗 来源:教育联展网 编辑:佚名 发布时间:2020-05-12 导语概要 深圳小码精灵编程培训让孩子爱上自主学习,成为未来世界的佼佼者.快速咨询为什么要学习少儿编程?所谓 ...

  7. 学习Python开发培训有用吗

    学习Python开发培训有用吗?这是目前很多人都比较关注的一个问题,Python语言在最近几年是广受IT互联网行业关注的, 下面我们就针对这问题来详细的分析一下. 学习Python开发培训有用吗?Py ...

  8. python操作mysql数据库依赖包_python安装mysql的依赖包mysql-python操作

    一般情况下,使用pip命令安装即可: [root@dthost27 ~]# pip install mysql-python 但是在实际工作环境中,往往会安装失败,这是因为系统缺少mysql的相关依赖 ...

  9. 无网络服务器(linux ubuntu),pip安装python科学计算所有需要包(packages)

    无网络服务器(linux ubuntu),pip安装python科学计算所有需要包(packages) # 在windows上打开anaconda,进入环境tab页,在base环境处单击,然后点开te ...

最新文章

  1. es6 --- 对任意对象部署可遍历接口
  2. php email,两种PHP邮件发送的方式
  3. 好看的表白墙LoveCards v1.0.4 源码(开源)
  4. 管理软件开发历程之一Coolite配置
  5. Windows版本nginx
  6. 程序员如何在技术浪潮的更迭中保持较高的成长速度 ?
  7. php post u,php 超全局变量 get post request
  8. java 局部内部类
  9. python全栈脱产第20天------常用模块---re模块和subprocess模块
  10. 解析数论 2: Abel求和法
  11. STC12C2052AD+TM1640+DS1302闹钟
  12. vulhub学习(1) ActiveMQ反序列化漏洞(CVE-2015-5254)复现
  13. Python4班平均成绩统计_重磅!长春12所热门高中高考成绩全部“亮相”!你看好谁?!...
  14. 非常好用的一款在线json转excel的工具
  15. CSMA/CD和CSMD/CA
  16. 基于高德地图SDK开发之地图显式
  17. 三、软考·系统架构师——计算机网络基础
  18. 导航栏钢琴键怎么实现?
  19. python初始画笔_Python自带Turtle画笔的原理
  20. 【力斩offer】一些面试常问的数学概念

热门文章

  1. swagger跟openAPI不同
  2. php mysql三_PHP 和 MySQL 基础教程(三)_MySQL
  3. 【分享】微软面试题及答案 (很需要开放性思维啊)
  4. SparkSQL JDBC连接
  5. 《睡眠革命》书中的精髓:如何让我们的睡眠变得高效?
  6. 百度搜索词API接口,淘宝搜索词API接口
  7. input js 离开事件_js控制input执行onchange事件
  8. 接收邮件服务器 imat,SkyCross iMAT射频技术为LTE和Wi-Fi助力实现4x4MIMO-射频/微波-与非网...
  9. 奈奎斯特 带宽 码元 比特
  10. 备忘录 memento