3-30

177 阅读1分钟

tkinter

  • 背景设置
#创建画布并将背景图添加到画布上,使用place布局格式(绝对坐标,单位像素)
photo = PhotoImage(file = "main_bg.gif")   # 加载图片
canvas = Canvas(wind,width = 500,height = 240)   #创建画布
canvas.create_image(250,120,anchor =CENTER,image = photo)   # 将图片置于画布上
canvas.place(x= 0,y=0)   #位置

StringIO

很多时候,数据读写不一定是文件,也可以在内存中读写。

StringIO顾名思义就是在内存中读写str。

要把str写入StringIO,我们需要先创建一个StringIO,然后,像文件一样写入即可:

>>> from io import StringIO
>>> f = StringIO()
>>> f.write('hello')
5
>>> f.write(' ')
1
>>> f.write('world!')
6
>>> print(f.getvalue())
hello world!

BytesIO

StringIO操作的只能是str,如果要操作二进制数据,就需要使用BytesIO。

BytesIO实现了在内存中读写bytes,我们创建一个BytesIO,然后写入一些bytes

>>> from io import BytesIO
>>> f = BytesIO()
>>> f.write('中文'.encode('utf-8'))
6
>>> print(f.getvalue())
b'\xe4\xb8\xad\xe6\x96\x87'