global img_
global laser_map
# 地图数据,从服务器收到数据,先解码
laser_map = base64.b64decode(msg)
file_image = open('build_map.png', 'wb+')
file_image.write(laser_map)
img_ = Image.open("build_map.png",)
width, height = img_.size
# 做旋转操作
if width < height:
self.rotate_flag = True
img_.rotate(90, expand=True).save('build_map_rotate.png')
else:
self.rotate_flag = False
img_.rotate(0, expand=True).save('build_map_rotate.png')
self.zoom_map_image = Image.open('build_map_rotate.png')
# PIL project tranform into PhotoImage project
laser_map = ImageTk.PhotoImage(self.zoom_map_image)
self.laser = self.canvas_map.create_image(self.canvas_center_x, self.canvas_center_y, anchor='center',image=laser_map)
self.canvas_map.place(x=0, y=0)
个人尝试用Canvas加载图片总是会闪烁,但发现用ttk的Frame和ttk的Label可以解决
我这里做了一个显示屏幕的小程序,可以看看有没有可以借鉴的地方。代码后面有关键部位的解释。
import tkinter
import tkinter.ttk
import _thread
import time
import PIL
import PIL.Image
import PIL.ImageGrab
import PIL.ImageTk
img = None
TkImg = None
st = time.perf_counter()
main = tkinter.Tk()
main.title('Your Screen')
F = tkinter.ttk.Frame(main, width=1920, height=1080)
L = tkinter.ttk.Label(F)
L.place(x=0, y=0)
F.place(x=0, y=0)
def RefreshImg():
global img, TkImg, main, L, st
img = PIL.ImageGrab.grab()
TkImg = PIL.ImageTk.PhotoImage(img)
L.config(image=TkImg)
main.title('Your Screen - Refresh Rate : ' + str((1/(time.perf_counter()-st)).__round__(1)) + 'fps')
st = time.perf_counter()
main.after(1, lambda:_thread.start_new_thread(RefreshImg, ()))
main.after(1, lambda:_thread.start_new_thread(RefreshImg, ()))
main.mainloop()
Label和Frame都必须是tkinter.ttk里面的,并且必须用place摆放部件
我用了多线程处理刷新图片,避免加载图片120ms左右导致窗口未响应
https://blog.csdn.net/u013468614/article/details/58689735
用for就行了:
for x in range([坐标列表]):
图片.place(x)
time.sleep(等待秒数,不要太长,不然会未响应)
窗口名.update()#窗口更新
canvas.after(2000, funmer)