qq_37602261 2018-10-23 08:22 采纳率: 0%
浏览 5754
已结题

Python使用tkinter实时动态显示图像闪烁问题?

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)
  • 写回答

4条回答

  • CarlGao4 2020-09-24 09:58
    关注

    个人尝试用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左右导致窗口未响应

    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题