Pixiv - KiraraShss
Python GUI
731 字
4 分钟
Python GUI
图形化
pysimplegui 5.0 版本开始收费,所以只能安装 5.0 之前的版本.
pip uninstall PySimpleGUIpip install PySimpleGUI==4.70.1import PySimpleGUI as sgimport os
def main(): # 定义历史记录文件路径 history_file = 'history.txt'
# 创建布局 layout = [ [sg.Text('File 1:'), sg.InputText(key='file1'), sg.FileBrowse(initial_folder=get_last_path(history_file, 0))], [sg.Text('File 2:'), sg.InputText(key='file2'), sg.FileBrowse(initial_folder=get_last_path(history_file, 1))], [sg.Text('File 3:'), sg.InputText(key='file3'), sg.FileBrowse(initial_folder=get_last_path(history_file, 2))], [sg.Text('File 4:'), sg.InputText(key='file4'), sg.FileBrowse(initial_folder=get_last_path(history_file, 3))], [sg.Button('OK')], ]
# 创建窗口 window = sg.Window('File Selector', layout)
while True: event, values = window.read()
if event == sg.WINDOW_CLOSED: break
if event == 'OK': # 保存历史记录 save_history(history_file, values['file1'], values['file2'], values['file3'], values['file4'])
# 弹出消息框 sg.popup('Hello World!') break
window.close()
def get_last_path(history_file, index): # 从历史记录文件中获取上次保存的路径 if os.path.exists(history_file): with open(history_file, 'r') as file: history = file.readlines() if len(history) > index: return history[index].strip() return ''
def save_history(history_file, *paths): # 将当前选择的路径保存到历史记录文件中 with open(history_file, 'w') as file: for path in paths: file.write(f'{path}\n')
if __name__ == '__main__': main()import PySimpleGUI as sg
sg.theme('Default1') # 设置当前主题# 界面布局,将会按照列表顺序从上往下依次排列,二级列表中,从左往右依此排列layout = [ [sg.Text('Some text on Row 1')], [sg.Text('Enter something on Row 2'), sg.InputText()], [sg.Button('Ok'), sg.Button('Cancel')] ]
# 创造窗口window = sg.Window('Window Title', layout)# 事件循环并获取输入值while True: event, values = window.read() if event in (None, 'Cancel'): # 如果用户关闭窗口或点击`Cancel` break print('You entered ', values[0])
window.close()
text_input = values[0]sg.popup('You entered', text_input) # 弹出窗口# 进度条显示import PySimpleGUI as sg
layout = [[sg.Text('A custom progress meter')], [sg.ProgressBar(1000, orientation='h', size=(20, 20), key='progressbar')], [sg.Cancel()]]
window = sg.Window('Custom Progress Meter', layout)progress_bar = window['progressbar']# loop that would normally do something usefulfor i in range(1000): # check to see if the cancel button was clicked and exit loop if clicked event, values = window.read(timeout=10) if event == 'Cancel' or event is None: break # update bar with loop value +1 so that bar eventually reaches the maximum progress_bar.UpdateBar(i + 1)# done with loop... need to destroy the window as it's still openwindow.close()import PySimpleGUI as sgsg.theme('Default1')# sg.popup('demo ok')# sg.popup_ok_cancel('PopupOKCancel')# sg.popup_error('PopupError') # Shows red error button# sg.popup_timed('PopupTimed') # Automatically closes# 例子:text = sg.popup_get_file('Please enter a file name')print(text)import PySimpleGUI as psgpsg.set_options(font=('Arial Bold', 16))layout = [ [psg.Text('Enter a num: '), psg.Input(key='-FIRST-')], [psg.Text('Enter a num: '), psg.Input(key='-SECOND-')], [psg.Text('Result : '), psg.Text(key='-OUT-')], [psg.Button("Add"), psg.Button("Sub"), psg.Exit()],]window = psg.Window('Calculato1r', layout, size=(715, 180))while True: event, values = window.read() print(event, values) if event == "Add": result = int(values['-FIRST-']) + int(values['-SECOND-']) if event == "Sub": result = int(values['-FIRST-']) - int(values['-SECOND-']) window['-OUT-'].update(result) if event == psg.WIN_CLOSED or event == 'Exit': breakwindow.close()import PySimpleGUI as psgpsg.set_options(font=('Arial Bold', 16))layout = [ [psg.Text('Name ', size=(15, 1)), psg.Input(key='-NM-', expand_x=True)], [psg.Text('Address ', size=(15, 1)), psg.Input(key='-AD-', expand_x=True)], [psg.Text('Email ID ', size=(15, 1)), psg.Input(key='-ID-', expand_x=True)], [psg.Text('You Entered '), psg.Text(key='-OUT-')], [psg.OK(), psg.Exit()],]window = psg.Window('Form', layout, size=(715, 200))while True: event, values = window.read() print(event, values) out = values['-NM-'] + ' ' + values['-AD-'] + ' ' + values['-ID-'] window['-OUT-'].update(out) if event == psg.WIN_CLOSED or event == 'Exit': breakwindow.close()import PySimpleGUI as psgtext = psg.popup_get_text('Enter your name', title="Textbox")print ("You entered: ", text)file=psg.popup_get_file('Select a file', title="File selector")print ("File selected", file)folder=psg.popup_get_folder('Get folder', title="Folder selector")print ("Folder selected",folder)ch = psg.popup_yes_no("Do you want to Continue?", title="YesNo")print ("You clicked", ch)ch = psg.popup_ok_cancel("Press Ok to proceed", "Press cancel to stop", title="OkCancel")if ch=="OK": print ("You pressed OK")if ch=="Cancel": print ("You pressed Cancel")psg.popup_no_buttons('You pressed', ch, non_blocking=True)psg.popup_auto_close('This window will Autoclose')# 选择主题import PySimpleGUI as sgtheme_name_list = sg.theme_list()sg.theme_previewer()sg.theme('Default1')layout = [[sg.Text('Theme Browser')], [sg.Text('Click a Theme color to see demo window')], [sg.Listbox(values=sg.theme_list(), size=(20, 12), key='-LIST-', enable_events=True)], [sg.Button('Exit')]]window = sg.Window('Theme Browser', layout)
while True: # Event Loop event, values = window.read() if event in (None, 'Exit'): break sg.theme(values['-LIST-'][0]) sg.popup_get_text('This is {}'.format(values['-LIST-'][0]))
window.close()支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!