【问题标题】:How to clear the graph drawn inPySimpleGUI?如何清除 PySimpleGUI 中绘制的图形?
【发布时间】:2021-10-20 03:27:33
【问题描述】:

有没有办法在重绘新图像之前清除 PySimpleGUI 中的图形?我注意到函数 window["-GRAPH-"].draw_image() 在程序运行一段时间时会导致严重的内存泄漏,因为它试图将图片堆叠在所有绘制的图像之上.

背景:

我的应用正在尝试显示来自网络摄像头的实时提要,同时还会在摄像头提要上进行一些绘图(取决于鼠标点击)。为了从相机源中检测鼠标点击事件,我使用 sg.Graph 来捕获鼠标位置。

sample_app_display:user label the box of object in a live camera feed

代码片段:

sg.Graph(853, (0, 480), (853, 0), key="-GRAPH-", change_submits=True, drag_submits=False)

...

camera = my_opencv_library(device=0)
while True:
    event, values = window.read(timeout=20)
    if event == "-GRAPH-":
        camera.update_coordinate(values["-GRAPH-"])

    # obtain live feed with runtime drawing (based on mouse click)
    frame = camera.get_frame()
    imgbytes = cv2.imencode(".png", frame)[1].tobytes()
    window["-GRAPH-"].draw_image(data=imgbytes, location=(0,0))

【问题讨论】:

  • 我想答案是可用的here。我希望这会有所帮助。
  • 感谢@A_Tiny_Speck_In_Programming,它确实解决了我的问题。

标签: python python-3.x user-interface pysimplegui


【解决方案1】:

擦除图形 - 删除以前使用图形方法“绘制”的所有图形

删除sg.Graph上的所有数字

window['-GRAPH-'].erase()

从图中删除由 id 表示的图形。

ids擦除sg.Graph上的指定图形

window['-GRAPH-'].delete_figure(ids)

ids 会在您调用绘图图元时提供给您,例如

ids = window["-GRAPH-"].draw_image(data=imgbytes, location=(0,0))

更新代码

sg.Graph(853, (0, 480), (853, 0), key="-GRAPH-", change_submits=True, drag_submits=False)

...

camera = my_opencv_library(device=0)
ids = None
while True:
    event, values = window.read(timeout=20)
    if event == "-GRAPH-":
        camera.update_coordinate(values["-GRAPH-"])

    # obtain live feed with runtime drawing (based on mouse click)
    frame = camera.get_frame()
    imgbytes = cv2.imencode(".png", frame)[1].tobytes()
    if ids is not None:
       window["-GRAPH-"].delete_figure(ids)
    ids = window["-GRAPH-"].draw_image(data=imgbytes, location=(0,0))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 2012-08-03
    • 2011-10-17
    • 2022-12-10
    • 1970-01-01
    相关资源
    最近更新 更多