Skip to content

Commit e4e987e

Browse files
committed
refactor: adapt windows
1 parent 431ff9e commit e4e987e

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

uiviewer/_device.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# -*- coding: utf-8 -*-
22

33
import abc
4+
import os
45
import traceback
56
import tempfile
6-
import os
7-
from typing import List, Dict, Union, Tuple, Optional
7+
from typing import List, Dict, Union, Tuple
88
from functools import cached_property # python3.8+
99

1010
from PIL import Image
@@ -56,14 +56,16 @@ def _display_size(self) -> Tuple:
5656
return self.hdc.display_size()
5757

5858
def take_screenshot(self) -> str:
59-
png_base64 = None
60-
with tempfile.NamedTemporaryFile(delete=True, suffix=".png") as f:
61-
path = f.name
62-
f.close()
59+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
60+
try:
61+
# adapt windows
62+
temp_file.close()
63+
path = temp_file.name
6364
self.hdc.screenshot(path)
64-
png_base64 = file2base64(path)
65-
os.remove(path)
66-
return png_base64
65+
return file2base64(path)
66+
finally:
67+
if os.path.exists(path):
68+
os.remove(path)
6769

6870
def dump_hierarchy(self) -> BaseHierarchy:
6971
packageName, pageName = self.hdc.current_app()

0 commit comments

Comments
 (0)