python - 如何在 PySimpleGUI 中单击时显示弹出窗口?
问题描述
我正在尝试显示一个弹出窗口,该窗口将在用户选择一个项目后使用 PySimple GUI 调用显示来自 API 的结果,但单击按钮时没有显示任何内容。
import PySimpleGUI as sg
# import PySimpleGUIQt as sg
import os.path
import PIL.Image
import io
#import imutils
import requests
import base64
import json
import base64
from detect_face_video import main
"""
"""
plate='f'
def convert_to_bytes(file_or_bytes, resize=None):
'''
Will convert into bytes and optionally resize an image that is a file or a base64 bytes object.
Turns into PNG format in the process so that can be displayed by tkinter
:param file_or_bytes: either a string filename or a bytes base64 image object
:type file_or_bytes: (Union[str, bytes])
:param resize: optional new size
:type resize: (Tuple[int, int] or None)
:return: (bytes) a byte-string object
:rtype: (bytes)
'''
if isinstance(file_or_bytes, str):
img = PIL.Image.open(file_or_bytes)
else:
try:
img = PIL.Image.open(io.BytesIO(base64.b64decode(file_or_bytes)))
except Exception as e:
dataBytesIO = io.BytesIO(file_or_bytes)
img = PIL.Image.open(dataBytesIO)
cur_width, cur_height = img.size
if resize:
new_width, new_height = resize
scale = min(new_height/cur_height, new_width/cur_width)
img = img.resize((int(cur_width*scale), int(cur_height*scale)), PIL.Image.ANTIALIAS)
with io.BytesIO() as bio:
img.save(bio, format="PNG")
del img
return bio.getvalue()
def update_plate(filename):
SECRET_KEY = 'sk_ed3d203bf4c9a7c2910ec0c0'
with open(filename, 'rb') as image_file:
img_base64 = base64.b64encode(image_file.read())
url = 'https://api.openalpr.com/v3/recognize_bytes?recognize_vehicle=1&country=us&secret_key=%s' % (SECRET_KEY)
r = requests.post(url, data = img_base64)
try:
global plate
plate=r.json()['results'][0]['plate'],
global Brand
Brand= r.json()['results'][0]['vehicle']['make_model'][0]['name'],
global Color
Color= r.json()['results'][0]['vehicle']['color'][0]['name'],
print(plate)
except:
print ('error')
# --------------------------------- Define Layout ---------------------------------
# First the window layout...2 columns
sg.theme('Dark Blue 3')
left_col = [[sg.Text('Folder'), sg.In(size=(25,1), enable_events=True ,key='-FOLDER-'), sg.FolderBrowse()],
[sg.Listbox(values=[], enable_events=True, size=(40,20),key='-FILE LIST-')],
[sg.Text('Resize to'), sg.In(key='-W-', size=(5,1)), sg.In(key='-H-', size=(5,1))],
[sg.Button("Resize", button_color=("white", "blue"), size=(6, 1))]]
# For now will only show the name of the file that was chosen
images_col = [[sg.Text('You choose from the list:')],
[sg.Text(size=(40,1), key='-TOUT-')],
[sg.Image(key='-IMAGE-')]]
# ----- Full layout -----
layout = [[sg.Column(left_col, element_justification='c'), sg.VSeperator(),sg.Column(images_col, element_justification='c')]]
# --------------------------------- Create Window ---------------------------------
window = sg.Window('Multiple Format Image Viewer', layout,resizable=True)
# ----- Run the Event Loop -----
# --------------------------------- Event Loop ---------------------------------
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == '-FOLDER-': # Folder name was filled in, make a list of files in the folder
folder = values['-FOLDER-']
try:
file_list = os.listdir(folder) # get list of files in folder
except:
file_list = []
fnames = [f for f in file_list if os.path.isfile(
os.path.join(folder, f)) and f.lower().endswith((".png", ".jpg", "jpeg", ".tiff", ".bmp"))]
window['-FILE LIST-'].update(fnames)
if event == 'Resize':
window['-IMAGE-'].update(data=convert_to_bytes(filename, resize=new_size))
elif event == '-FILE LIST-': # A file was chosen from the listbox
try:
filename = os.path.join(values['-FOLDER-'], values['-FILE LIST-'][0])
window['-TOUT-'].update(filename)
if values['-W-'] and values['-H-']:
new_size = int(values['-W-']), int(values['-H-'])
else:
new_size = None
window['-IMAGE-'].update(data=convert_to_bytes(filename, resize=new_size))
main(filename)
update_plate(filename)
print(plate)
except Exception as E:
print(f'** Error {E} **')
pass # something weird happened making the full filename
# --------------------------------- Close & Exit ---------------------------------
window.close()
解决方案
在 Popup 调用中设置保持在顶部为我创建了顶部的窗口。只需添加
sg.Popup("license plate" , plate , keep_on_top=True)
但是,如果您单击后面的窗口,因为它也设置了保持在顶部,它将覆盖您的弹出窗口。
推荐阅读
- php - 集成 php 和 google sheet api 时出错
- java - restMockMvc - 预期:一个包含 <1.0F> 的集合,但:是 <1.0>
- docker - Docker Compose ECS 负载均衡器未配置
- linux - AWS CLI S3 CP --recursive 函数在控制台中有效,但在 .sh 文件中无效
- flutter - 如何使用颤振和 Firestore 创建聊天室
- python - 带有 setWordWrap(True) 的 QLabel 在放置在 QListWidget 中时不会垂直调整大小,即使 resizeMode 已设置为 Adjust
- javascript - 在装饰器模式中实现委托
- javascript - 当超过 1 个 div 附加时,我从数据库中检索的下拉选项会增加
- javascript - 如何将 React 组件的自定义钩子与“map”一起使用
- batch-file - 使用路径中具有多个通配符的批处理脚本查找文件