请看下我的wxpython爬虫天气程序哪里错了

我的代码到底哪里有问题,麻烦解答一下谢谢


```python
import wx
import requests
from bs4 import BeautifulSoup

# 定义天气图标字典
weather_icons = {
    '晴': '☀️',
    '多云': '⛅',
    '阴': '☁️',
    '小雨': '🌧️',
    '中雨': '🌧️',
    '大雨': '🌧️',
    '雷阵雨': '⛈️',
    '暴雨': '⛈️',
    '雪': '❄️',
    '霾': '🌫️'
}

class WeatherPanel(wx.Panel):

    # 中国天气网城市编码映射表
    city_codes = {
        '北京': '101010100',
        '上海': '101020100',
        '广州': '101280101',
        '深圳': '101280601',
        '杭州': '101210101',
        '南京': '101190101',
        '武汉': '101200101',
        '成都': '101270101',
        '重庆': '101040100'
    }

    def __init__(self, parent):
        super().__init__(parent)
        self.weather_label = wx.StaticText(self, label='', style=wx.ALIGN_CENTER)
        font = wx.Font(30, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
        self.weather_label.SetFont(font)
        # 添加按钮用于选择服务器区域
        asia_button = wx.Button(self, label='亚服')
        europe_button = wx.Button(self, label='欧服')
        america_button = wx.Button(self, label='美服')
        china_button = wx.Button(self, label='中服')
        self.Bind(wx.EVT_BUTTON, self.on_asia_button_click, asia_button)
        self.Bind(wx.EVT_BUTTON, self.on_europe_button_click, europe_button)
        self.Bind(wx.EVT_BUTTON, self.on_america_button_click, america_button)
        self.Bind(wx.EVT_BUTTON, self.on_china_button_click, china_button)
        # 布局界面
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(asia_button, proportion=1, flag=wx.EXPAND|wx.ALL, border=10)
        hbox.Add(europe_button, proportion=1, flag=wx.EXPAND|wx.ALL, border=10)
        hbox.Add(america_button, proportion=1, flag=wx.EXPAND|wx.ALL, border=10)
        hbox.Add(china_button, proportion=1, flag=wx.EXPAND|wx.ALL, border=10)
        vbox.Add(self.weather_label, proportion=1, flag=wx.EXPAND|wx.ALL, border=10)
        vbox.Add(hbox, proportion=1, flag=wx.ALIGN_CENTER|wx.ALL, border=10)
        self.SetSizer(vbox)

    def update_weather(self, city_code):
        # 获取天气信息
        r = requests.get(f'http://www.weather.com.cn/data/cityinfo/{city_code}.html')
        data = r.json()
        weather_data = data['weatherinfo']
        temperature = weather_data['temp'] + '℃'
        code = weather_data['weather']
        icon = weather_icons.get(code, '')
        self.weather_label.SetLabelText(f'{icon} {temperature}')

    def on_asia_button_click(self, event):
        self.update_weather('101010100')

    def on_europe_button_click(self, event):
        self.update_weather('101020100')

    def on_america_button_click(self, event):
        self.update_weather('101280101')

    def on_china_button_click(self, event):
        # 打开城市选择对话框
        dialog = wx.SingleChoiceDialog(None, '请选择要查询的城市', '城市选择', list(self.city_codes.keys()))
        if dialog.ShowModal() == wx.ID_OK:
            city = dialog.GetStringSelection()
            city_code = self.city_codes[city]
            self.update_weather(city_code)

class WeatherFrame(wx.Frame):
    def __init__(self):
        super().__init__(None, title='Game Engine Demo', size=(640, 480))
        self.panel = WeatherPanel(self)

if __name__ == '__main__':
    app = wx.App()
    frame = WeatherFrame()
    frame.Show()
    app.MainLoop()

```

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 你可以看下这个问题的回答https://ask.csdn.net/questions/7640719
  • 你也可以参考下这篇文章:wxpython实现一个登录窗口
  • 除此之外, 这篇博客: 手把手教你用 wxPython 设计一个可以弹琴的计算器中的 3. 了解事件驱动,探索鼠标事件及其绑定 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    wx是基于事件驱动的,每个事件都需要绑定事件函数。一旦有事件发生,则触发对应的事件函数执行。

    pyCalculator_2.py

    #-*- coding: utf-8 -*-
    
    import wx
    
    """学习wx.Button和wx.StaticText控件,探索鼠标事件以及绑定事件函数"""
    
    APP_TITLE = '计算器' # 桌面程序的标题
    APP_ICON = 'calculator.ico' # 桌面程序图标
    
    class mainFrame(wx.Frame):
        """桌面程序主窗口类,继承自wx.Frame类"""
        
        def __init__(self):
            """构造函数"""
            
            style = wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX | wx.MINIMIZE_BOX | wx.SIMPLE_BORDER
            wx.Frame.__init__(self, parent=None, id=-1, title=APP_TITLE, style=style)
            
            self.SetBackgroundColour((240, 240, 240)) # 设置窗口背景色
            self.SetSize((640, 480)) # 设置窗口大小
            self.Center() # 设置窗口屏幕居中
            self.SetIcon(wx.Icon(APP_ICON, wx.BITMAP_TYPE_ICO) ) # 设置图标(没有图标文件的话,会弹出警告信息)
            
            btn = wx.Button(self, -1, '按钮', pos=(100,100), size=(80,40)) # 添加按钮
            self.st = wx.StaticText(self, -1, '', pos=(200,110), size=(200, -1)) # 添加静态文本控件,用于显示信息
            
            btn.Bind(wx.EVT_LEFT_DOWN, self.onLeftDown) # 绑定鼠标左键按下事件,事件发生时,调用self.onLeftDown()
            btn.Bind(wx.EVT_LEFT_UP, self.onLeftUp) # 绑定鼠标左键弹起事件,事件发生时,调用self.onLeftUp()
            self.Bind(wx.EVT_MOUSE_EVENTS, self.onMouse) # 在窗口上绑定所有的鼠标事件
            
        def onLeftDown(self, evt):
            """响应鼠标左键按下"""
            
            self.st.SetLabel('左键在按钮上按下了')
            
        def onLeftUp(self, evt):
            """响应鼠标左键弹起"""
            
            self.st.SetLabel('左键从按钮上弹起了')
            
        def onMouse(self, evt):
            """响应所有的鼠标事件"""
            
            if evt.EventType == 10036:
                tip = '鼠标移动: (%d, %d)'%(evt.x, evt.y)
            elif evt.EventType == 10030:
                tip = '左键按下'
            elif evt.EventType == 10031:
                tip = '左键弹起'
            elif evt.EventType == 10034:
                tip = '右键按下'
            elif evt.EventType == 10035:
                tip = '右键弹起'
            elif evt.EventType == 10045:
                vector = evt.GetWheelRotation()
                tip = '滚轮滚动: %d'%vector
            else:
                tip = '其他鼠标事件: %d'%evt.EventType
            
            self.st.SetLabel(tip)
    
    
    class mainApp(wx.App):
        def OnInit(self):
            self.SetAppName(APP_TITLE)
            self.Frame = mainFrame()
            self.Frame.Show()
            return True
    
    #----------------------------------------------------------------------
    if __name__ == "__main__":
        app = mainApp() # 创建应用程序
        app.MainLoop() # 事件循环
    
  • 您还可以看一下 尹成老师的python GUI教程课程中的 wxpython下拉菜单绑定事件小节, 巩固相关知识点

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^