明白了,你想了解 PySimpleGUI 中与 popup_get_*
相关的函数。我给你整理一个完整列表、用法说明和示例。
PySimpleGUI 中 popup_get_*
系列函数
popup_get_*
系列函数用于弹出输入对话框,让用户输入各种类型的数据。这些函数都是 快捷版 Popup + Input,返回用户输入的值(字符串或数值),简化了手动布局窗口的操作。
1️⃣ popup_get_text
- 作用:弹出一个文本输入框,让用户输入字符串
- 函数签名:
sg.popup_get_text(message, title='', default_text='', size=(None, None), font=None, **kwargs)
- 返回值:用户输入的字符串;点击取消返回
None
- 示例:
import PySimpleGUI as sg
name = sg.popup_get_text('请输入你的名字:', '文本输入')
if name:
sg.popup(f'你好,{name}!')
2️⃣ popup_get_file
- 作用:弹出文件选择对话框,让用户选择一个文件
- 函数签名:
sg.popup_get_file(message='', title='', default_path='', file_types=(("ALL Files","*.*"),), save_as=False, **kwargs)
- 参数:
save_as
:是否为保存文件(默认False
)file_types
:文件类型过滤
- 返回值:用户选择的文件路径;取消返回
None
- 示例:
file_path = sg.popup_get_file('请选择文件:', '文件选择')
if file_path:
sg.popup(f'选择的文件是:{file_path}')
3️⃣ popup_get_folder
- 作用:弹出文件夹选择对话框,让用户选择一个文件夹
- 函数签名:
sg.popup_get_folder(message='', title='', default_path='', **kwargs)
- 返回值:用户选择的文件夹路径;取消返回
None
- 示例:
folder = sg.popup_get_folder('请选择一个文件夹:', '文件夹选择')
if folder:
sg.popup(f'选择的文件夹是:{folder}')
4️⃣ popup_get_date
- 作用:弹出日期选择对话框(PySimpleGUI 内置日期选择器,可能需要
PySimpleGUI
版本支持) - 函数签名:
sg.popup_get_date(message='', title='', no_titlebar=False, **kwargs)
- 返回值:用户选择的日期(元组
year, month, day
);取消返回None
- 示例:
date = sg.popup_get_date('请选择日期:', '日期选择')
if date:
sg.popup(f'选择的日期是:{date[0]}-{date[1]}-{date[2]}')
🔹 总结
函数 | 类型 | 返回值 | 备注 |
---|---|---|---|
popup_get_text | 文本输入 | 字符串 | 快捷文本输入弹窗 |
popup_get_file | 文件选择 | 文件路径 | 可设置文件类型过滤和保存模式 |
popup_get_folder | 文件夹选择 | 文件夹路径 | 快捷选择目录 |
popup_get_date | 日期选择 | (year, month, day) | 内置日期选择对话框 |
这些函数适合快速原型开发或简单工具,无需自己设计窗口布局,便于弹出单次输入对话框。
发表回复