Python脚本无法正确生成

时间:2016-03-22 19:18:14

标签: python

我在.py中有这个脚本会抛出错误。

import sys

import wx                  # This module uses the new wx namespace
import wx.html
import wx.lib.wxpTag

import urllib2
import json
f = urllib2.urlopen('http://api.wunderground.com/api/b5586703ce3fb523/geolookup/conditions/q/04002.json')
json_string = f.read()
parsed_json = json.loads(json_string)
location = parsed_json['location']['city']
temp_f = parsed_json['current_observation']['temp_f']
temp_feels = parsed_json['current_observation']['feelslike_f']
winds = parsed_json['current_observation']['wind_string']
humidity = parsed_json['current_observation']['relative_humidity']
windchill = parsed_json['current_observation']['windchill_f']
visibility = parsed_json['current_observation']['visibility_mi']
precipitation = parsed_json['current_observation']['precip_today_in']
conditions = parsed_json['current_observation']['weather']
print "Current temperature in %s is: %s" % (location, temp_f)
print "Feels Like: %sF" % (temp_feels)
print "Winds coming %s " % (winds)
print "Humidity: %s " % (humidity)
print "Windchill: %sF " % (windchill)
print "Visibility: %s Miles " % (visibility)
print "Precipitation Today: %s Inches " % (precipitation)
print "Conditions: %s " % (conditions)
f.close()



#---------------------------------------------------------------------------

class MyAboutBox(wx.Dialog):
    text = '''
<html>
<body bgcolor="#AC76DE">
<center><table bgcolor="#458154" width="100%%" cellspacing="0"
cellpadding="0" border="1">
<tr>
    <td align="center">
    <h1>wxPython %s</h1>
    (%s)<br>
    Running on Python %s<br>
     print "Conditions: %s " % (conditions)
    </td>
</tr>
</table>



<p><wxp module="wx" class="Button">
    <param name="label" value="Okay">
    <param name="id"    value="ID_OK">
</wxp></p>
</center>
</body>
</html>
'''
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, -1, 'About the wxPython demo',)
        html = wx.html.HtmlWindow(self, -1, size=(420, -1))
        if "gtk2" in wx.PlatformInfo or "gtk3" in wx.PlatformInfo:
            html.SetStandardFonts()
        py_version = sys.version.split()[0]
        txt = self.text % (wx.VERSION_STRING,
                           ", ".join(wx.PlatformInfo[1:]),
                           py_version
                           )
        html.SetPage(txt)
        btn = html.FindWindowById(wx.ID_OK)
        ir = html.GetInternalRepresentation()
        html.SetSize( (ir.GetWidth()+25, ir.GetHeight()+25) )
        self.SetClientSize(html.GetSize())
        self.CentreOnParent(wx.BOTH)

#---------------------------------------------------------------------------



if __name__ == '__main__':
    app = wx.App()
    dlg = MyAboutBox(None)
    dlg.ShowModal()
    dlg.Destroy()
    app.MainLoop()

我尝试做的是在对话框中显示天气信息但由于某种原因导致错误

Traceback (most recent call last):
  File "C:/Users/david/Desktop/Test1.py", line 85, in <module>
    dlg = MyAboutBox(None)
  File "C:/Users/david/Desktop/Test1.py", line 70, in __init__
    py_version
TypeError: not enough arguments for format string

我是python的新手,所以我似乎无法弄清楚导致错误的问题。

2 个答案:

答案 0 :(得分:0)

在以下行中 您正在尝试使用3个变量格式化self.text,但是 self.text实际上需要4。

txt = self.text % (wx.VERSION_STRING,
                   ", ".join(wx.PlatformInfo[1:]),
                   py_version
                  )

答案 1 :(得分:0)

通过self.text设置txt时,您缺少两个参数。具体来说,您需要提供Conditions: %s " % (conditions)

的参数

所以,你需要像

这样的东西
txt = self.text % (wx.VERSION_STRING,
                   ", ".join(wx.PlatformInfo[1:]),
                   py_version,
                   condition,
                   condition_value
                   ) 

其中condition和condition_value之前设置在某处。