PySimpleGUI开发求助:实现Multiline组件旁基于输入内容的动态图片切换
解决PySimpleGUI中Multiline旁动态切换图片的问题
我明白你想要在Multiline组件旁边添加图片,并根据输入内容动态切换的需求。下面是具体的实现方案,我已经基于你的代码做了修改和优化:
核心改动说明
- 布局调整:将Multiline和Image组件放在同一行,实现并排显示
- 图片切换逻辑:在SEND事件中判断输入内容,更新Image组件的显示文件
- 默认状态设置:初始时加载默认图片(image1.png)
修改后的完整代码
import PySimpleGUI as sg import pathlib sg.theme('DarkAmber') WIN_W = 90 WIN_H = 25 file = None # 菜单布局保持不变 menu_layout = [['File', ['New (Ctrl+N)', 'Open (Ctrl+O)', 'Save (Ctrl+S)', 'Save As', '---', 'Exit']], ['Tools', ['Word Count']], ['Help', ['About']]] # 修改布局:在Multiline右侧添加Image组件,默认显示image1.png layout = [ [sg.Menu(menu_layout)], [sg.Text('Terminal', font=('Consolas', 10), size=(WIN_W, 1), key='_INFO_')], [sg.Text('Write here'), sg.Input(key='-IN-')], # 新增Image组件,设置key用于后续更新,size可根据你的图片尺寸调整 [sg.Multiline(font=('Consolas', 12), size=(WIN_W, WIN_H), disabled=True, autoscroll=True, key='-OUTPUT-'), sg.Image(filename='image1.png', key='-IMAGE-', size=(200, 200))], [sg.Button('SEND', bind_return_key=True), sg.Button('EXIT'), sg.Button('SAVE FILE')] ] window = sg.Window('sedia', layout, finalize=True) # 以下函数保持不变 def new_file(): '''Reset body and info bar, and clear filename variable''' window['-OUTPUT-'].update(value='') window['_INFO_'].update(value='Terminal') file = None return file def open_file(): '''Open file and update the infobar''' filename = sg.popup_get_file('Open', no_window=True) if filename: file = pathlib.Path(filename) window['-OUTPUT-'].update(value=file.read_text()) window['_INFO_'].update(value=file.absolute()) return file def save_file(file): '''Save file instantly if already open; otherwise use `save-as` popup''' if file: file.write_text(values.get('-OUTPUT-')) else: save_file_as() def save_file_as(): '''Save new file or save existing file with another name''' filename = sg.popup_get_file('Save As', save_as=True, no_window=True) if filename: file = pathlib.Path(filename) file.write_text(values.get('-OUTPUT-')) window['_INFO_'].update(value=file.absolute()) return file def word_count(): '''Display estimated word count''' words = [w for w in values['-OUTPUT-'].split(' ') if w != '\n'] word_count = len(words) sg.popup_no_wait('Word Count: {:,d}'.format(word_count)) def about_me(): '''A short, pithy quote''' sg.popup_no_wait('"ok boomer." - Andreo') while True: event, values = window.read() if event in ('New (Ctrl+N)', 'n:78'): file = new_file() if event in ('Open (Ctrl+O)', 'o:79'): file = open_file() if event in ('Save (Ctrl+S)', 's:83'): save_file(file) if event == 'SAVE FILE': save_file(file) if event in ('Save As',): file = save_file_as() if event in ('Word Count',): word_count() if event in ('About',): about_me() if (event == sg.WINDOW_CLOSE_ATTEMPTED_EVENT or event == 'EXIT') and sg.popup_yes_no('Do you really want to EXIT?') == 'Yes': break if event == 'SEND': input_text = values['-IN-'].strip() window['-OUTPUT-'].print(input_text) # 根据输入内容切换图片 if input_text == 'home': window['-IMAGE-'].update(filename='image3.png') elif input_text == 'dog': window['-IMAGE-'].update(filename='image2.png') else: # 输入其他内容时回到默认图片 window['-IMAGE-'].update(filename='image1.png') print(input_text) window.close()
注意事项
- 请确保你的图片文件(
image1.png、image2.png、image3.png)和脚本在同一目录下,或者使用绝对路径指定文件位置(比如C:/images/image1.png) - 你可以根据实际图片尺寸调整Image组件的
size参数,避免图片拉伸或显示不全 - 如果输入内容需要不区分大小写(比如输入Home也触发切换),可以把判断条件改成
input_text.lower() == 'home'
内容的提问来源于stack exchange,提问作者Andrea Obbialero
相关产品推荐
相关产品推荐

