Python脚本未正确生成
我在.py中有这个脚本,它抛出了一个错误Python脚本未正确生成,python,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/condit
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()
导入系统
导入wx#此模块使用新的wx命名空间
导入wx.html
导入wx.lib.wxpTag
导入urllib2
导入json
f=urllib2.urlopen('http://api.wunderground.com/api/b5586703ce3fb523/geolookup/conditions/q/04002.json')
json_string=f.read()
parsed_json=json.loads(json_字符串)
location=parsed_json['location']['city']
temp_f=已解析的json['current_observation']['temp_f']
temp_feels=parsed_json['current_observation']['feelslike_f']
winds=parsed_json['current_observation']['wind_string']
湿度=已解析的json[“当前观察”][“相对湿度”]
windchill=parsed_json['current_observation']['windchill_f']
可见性=已解析的json['current\u observation']['visibility\u mi']
降水量=已解析的json['current_observation']['precip_today_in']
条件=已解析的json['current\u observation']['weather']
打印“%s中的当前温度为:%s”%(位置,温度)
打印“感觉像:%sF”%(临时感觉)
打印“风向%s”%(风向)
打印“湿度:%s”%(湿度)
打印“Windchill:%sF”%(Windchill)
打印“可见性:%s英里”%(可见性)
打印“今天降水量:%s英寸”%(降水量)
打印“条件:%s”%(条件)
f、 关闭()
#---------------------------------------------------------------------------
类MyAboutBox(wx.Dialog):
文本='''
wxPython%s
(%s)在Python%s上运行
打印“条件:%s”%(条件) ''' 定义初始化(自身,父级): Dialog.\uuuu init\uuuuu(self,parent,-1,‘关于wxPython演示’,) html=wx.html.HtmlWindow(self,-1,size=(420,-1)) 如果wx.PlatformInfo中的“gtk2”或wx.PlatformInfo中的“gtk3”: html.SetStandardFonts() py_version=sys.version.split()[0] txt=self.text%(wx.VERSION\u字符串, “,”.join(wx.PlatformInfo[1:]), py_版本 ) html.SetPage(txt) btn=html.FindWindowById(wx.ID\u确定) ir=html.GetInternalRepresentation() SetSize((ir.GetWidth()+25,ir.GetHeight()+25)) self.SetClientSize(html.GetSize()) self.CentreOnParent(wx.BOTH) #--------------------------------------------------------------------------- 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': app=wx.app() dlg=MyAboutBox(无) 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
回溯(最近一次呼叫最后一次):
文件“C:/Users/david/Desktop/Test1.py”,第85行,在
dlg=MyAboutBox(无)
文件“C:/Users/david/Desktop/Test1.py”,第70行,在__
py_版本
TypeError:格式字符串的参数不足
我对python非常陌生,所以我似乎无法找出导致错误的问题 在下一行
您正在尝试使用3个变量格式化self.text
,但是
self.text
实际上需要4个
txt = self.text % (wx.VERSION_STRING,
", ".join(wx.PlatformInfo[1:]),
py_version
)
通过self.text设置txt时,缺少两个参数。具体来说,您需要为
条件:%s“%(条件)
所以,你需要像
txt = self.text % (wx.VERSION_STRING,
", ".join(wx.PlatformInfo[1:]),
py_version,
condition,
condition_value
)
其中condition和condition_值是在之前的某个地方设置的。您的
txt=self.text%…
行有三个参数,但您的self.text
字符串中似乎有超过百分之三的符号。请尝试使计数匹配。