You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PySimpleGUI宝可梦助手:Listbox右侧添加精灵球图片异常问题

解决PySimpleGUI中精灵球图片显示异常问题

问题原因

图片显示异常通常源于三点:

  • 未对图片做缩放处理,导致超出布局范围或显示不全
  • 布局结构不合理,元素挤压引发图片变形
  • 图片路径错误或加载方式不当

解决方案

1. 安装依赖库

先安装Pillow用于图片缩放:

pip install pillow

2. 修改代码实现正确加载与布局

以下是修复后的完整代码,关键改动已标注:

import PySimpleGUI as sg
import pandas as pd
from PIL import Image, ImageTk

pokemon_data = pd.read_csv('pokemon/pokemondata.csv')

# 缩放精灵球图片到指定大小
def load_pokeball_image(size=(100, 100)):
    try:
        # 替换为你的精灵球图片实际路径
        img = Image.open('pokeball.png')
        img = img.resize(size, Image.Resampling.LANCZOS)
        return ImageTk.PhotoImage(img)
    except Exception as e:
        print(f"加载图片失败: {e}")
        return None

sg.theme("DarkRed2")

# 初始化图片
pokeball_img = load_pokeball_image()

# 重构布局,将Listbox和图片横向排列,避免挤压
layout = [
    [sg.Text('What pokemon are you against'), sg.InputText()],
    [sg.Button('Enter'), sg.Button('Exit')],
    [sg.Text('Pokemon-tips:')],
    [
        sg.Listbox(values=[], size=(30, 10), key='pokemon_listbox'),
        sg.Image(data=pokeball_img, key='pokeball_img', size=(100, 100))
    ]
]

# 设置窗口固定大小,防止元素变形
window = sg.Window('Pokemon Helper', layout, resizable=False)


def get_pokemon_tips(pokemon_name):
    pokemon_name = pokemon_name.lower()

    user_pokemon = pokemon_data[pokemon_data['Name'].str.lower() == pokemon_name]

    if user_pokemon.empty:
        return ['Ingen matchande Pokemon hittades.']

    user_type = user_pokemon['Type 1'].values[0]

    weak_to = []
    if user_type == 'Normal':
        weak_to = ['Fighting']
    elif user_type == 'Fire':
        weak_to = ['Water', 'Ground', 'Rock']
    elif user_type == 'Water':
        weak_to = ['Grass', 'Electric']
    elif user_type == 'Grass':
        weak_to = ['Fire', 'Ice', 'Poison', 'Flying', 'Bug']
    elif user_type == 'Electric':
        weak_to = ['Ground']
    elif user_type == 'Ice':
        weak_to = ['Fire', 'Fighting', 'Rock', 'Steel']
    elif user_type == 'Fighting':
        weak_to = ['Flying', 'Psychic', 'Fairy']
    elif user_type == 'Poison':
        weak_to = ['Ground', 'Psychic']
    elif user_type == 'Ground':
        weak_to = ['Water', 'Grass', 'Ice']
    elif user_type == 'Flying':
        weak_to = ['Electric', 'Ice', 'Rock']
    elif user_type == 'Psychic':
        weak_to = ['Bug', 'Ghost', 'Dark']
    elif user_type == 'Bug':
        weak_to = ['Flying', 'Rock', 'Fire']
    elif user_type == 'Rock':
        weak_to = ['Water', 'Grass', 'Fighting', 'Ground', 'Steel']
    elif user_type == 'Ghost':
        weak_to = ['Ghost', 'Dark']
    elif user_type == 'Dragon':
        weak_to = ['Ice', 'Dragon', 'Fairy']
    elif user_type == 'Dark':
        weak_to = ['Fighting', 'Bug', 'Fairy']
    elif user_type == 'Steel':
        weak_to = ['Fire', 'Fighting', 'Ground']
    elif user_type == 'Fairy':
        weak_to = ['Poison', 'Steel']

    better_pokemon = pokemon_data[pokemon_data['Type 1'].isin(weak_to)]

    better_pokemon = better_pokemon.sort_values(['Attack', 'Defense', 'HP'], ascending=[False, False, False])

    return better_pokemon['Name'].values[:5][::-1]


while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    elif event == 'Enter':
        pokemon_name = values[0]
        pokemon_tips = get_pokemon_tips(pokemon_name)
        window['pokemon_listbox'].update(pokemon_tips)

window.close()

关键改动说明

  • 图片缩放:用Pillow将图片缩放到固定尺寸(100x100),确保显示大小匹配布局,避免过大或显示不全
  • 布局调整:将Listbox和Image放在同一行横向排列,避免元素挤压变形
  • 窗口固定:设置resizable=False防止窗口拉伸导致图片变形
  • 错误处理:添加图片加载异常捕获,方便排查路径问题

注意事项

  • 确保精灵球图片路径正确,可修改load_pokeball_image中的路径为实际存放路径
  • 若需动态更换图片,可在Enter事件中重新调用load_pokeball_image并执行window['pokeball_img'].update(data=new_img)更新

内容的提问来源于stack exchange,提问作者Raphael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 04:24:57