使用 PySimpleGui 强制用户 select 一个文件?
Force users to select a file with PySimpleGui?
我是 PySimpleGui 的新手,我搜索了所有可能对我有帮助的食谱,但一无所获。
我想做的是:用户将 select 一个文件,单击一个按钮,应用程序将对该文件执行某些操作。这是我目前所拥有的:
layout = [[sg.Text('Sistema'), sg.InputText(key='-file1-'), sg.FileBrowse()], [sg.Button("Go")]]
window = sg.Window('Test', layout)
while True:
event, values = window.read()
if event == "Go":
*do something with file1*
我想要的是:
layout = [[sg.Text('Sistema'), sg.InputText(key='-file1-'), sg.FileBrowse()], [sg.Button("Go")]]
window = sg.Window('Test', layout)
while True:
event, values = window.read()
if values["-file1-"] == "":
print("You need to choose a file!")
*allow users to select a new file without closing the window*
if event == "Go":
*do something with file1*
我需要做什么?如果我添加一个 break 语句,它会离开 while 循环并关闭 window。我需要它来允许用户 select 一个新文件并再次阅读 window。可能吗?
在事件循环中事件 Go
时确认文件名是否正确。
这里,Cancel
取消选择一个文件,也停止Go
。
from pathlib import Path
import PySimpleGUI as sg
sg.theme("DarkBlue")
layout = [
[sg.Text('Sistema'), sg.InputText(key='-file1-'), sg.FileBrowse()],
[sg.Button("Go")],
]
window = sg.Window('Test', layout)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == "Go":
filename = values['-file1-']
while True:
if not Path(filename).is_file():
if filename == '':
sg.popup_ok('Select a file to go !')
else:
sg.popup_ok('File not exist !')
filename = sg.popup_get_file("", no_window=True)
if filename == '':
break
window['-file1-'].update(filename)
else:
print('File is ready !')
break
window.close()
我是 PySimpleGui 的新手,我搜索了所有可能对我有帮助的食谱,但一无所获。
我想做的是:用户将 select 一个文件,单击一个按钮,应用程序将对该文件执行某些操作。这是我目前所拥有的:
layout = [[sg.Text('Sistema'), sg.InputText(key='-file1-'), sg.FileBrowse()], [sg.Button("Go")]]
window = sg.Window('Test', layout)
while True:
event, values = window.read()
if event == "Go":
*do something with file1*
我想要的是:
layout = [[sg.Text('Sistema'), sg.InputText(key='-file1-'), sg.FileBrowse()], [sg.Button("Go")]]
window = sg.Window('Test', layout)
while True:
event, values = window.read()
if values["-file1-"] == "":
print("You need to choose a file!")
*allow users to select a new file without closing the window*
if event == "Go":
*do something with file1*
我需要做什么?如果我添加一个 break 语句,它会离开 while 循环并关闭 window。我需要它来允许用户 select 一个新文件并再次阅读 window。可能吗?
在事件循环中事件 Go
时确认文件名是否正确。
这里,Cancel
取消选择一个文件,也停止Go
。
from pathlib import Path
import PySimpleGUI as sg
sg.theme("DarkBlue")
layout = [
[sg.Text('Sistema'), sg.InputText(key='-file1-'), sg.FileBrowse()],
[sg.Button("Go")],
]
window = sg.Window('Test', layout)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == "Go":
filename = values['-file1-']
while True:
if not Path(filename).is_file():
if filename == '':
sg.popup_ok('Select a file to go !')
else:
sg.popup_ok('File not exist !')
filename = sg.popup_get_file("", no_window=True)
if filename == '':
break
window['-file1-'].update(filename)
else:
print('File is ready !')
break
window.close()