#encoding:utf-8
#!/user/bin/python
import urllib.request
import city
import time
import json
import tkinter as tk
import win32api
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.create_widgets()
def create_widgets(self):
self.lable1=tk.Label()
self.lable1.pack(side="top")
self.lable1["text"]="请输入一个城市 \n 例如:东莞/北京/上海……"
self.CityInputTEXT=tk.Text()
self.CityInputTEXT.pack(side='bottom')
self.button1=tk.Button()
self.button1.pack(side="bottom")
self.button1['text']="查询"
if self.button1['command'] :
return self.CityInputTEXT["text"]
def chaxun(CTname):
cityname=city.city.get(CTname)
if cityname:
url = "http://www.weather.com.cn/data/cityinfo/%s.html" % cityname
content = urllib.request.urlopen(url).read().decode("utf-8")
data = json.loads(content)
result = data['weatherinfo']
Weather = json.loads(content)
cityZIDIAN = Weather["weatherinfo"]
cityInformation = ("%s\n%s\n%s ~ %s") % (cityZIDIAN['city'], cityZIDIAN['weather'],result['temp1'],result['temp2'])
return cityInformation
else:
win32api.MessageBox(0, "There are no this City\n\a没有該城市!", "今天天气")
root = tk.Tk()
app=Application(root)
app.mainloop()
运行后是这样的
我想要 使用查询功能
在app.mainloop()上面加了一句
win32api.MessageBox(0,app.chaxun(app.create_widgets()),"")
结果就运行错误 如图
希望大神能快点解答
小弟在此谢过
def chaxun(CTname): //函数参数数目为1,调用时,传了2个参数,不匹配。
改了一下 把def chaxun(CTname):
改为 def chaxun(self,CTname):
出现了一下错误
上面图片错了 应该是这个
更改后的代码
#encoding:utf-8
#!/user/bin/python
import urllib.request
import city
import time
import json
import tkinter as tk
import win32api
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.create_widgets()
def create_widgets(self):
self.lable1=tk.Label()
self.lable1.pack(side="top")
self.lable1["text"]="请输入一个城市 \n 例如:东莞/北京/上海……"
self.CityInputTEXT=tk.Text()
self.CityInputTEXT.pack(side='bottom')
self.button1=tk.Button()
self.button1.pack(side="bottom")
self.button1['text']="查询"
if self.button1['command'] :
return self.CityInputTEXT["text"]
def chaxun(self,CTname):
cityname=city.city.get(CTname)
if cityname:
url = "http://www.weather.com.cn/data/cityinfo/%s.html" % cityname
content = urllib.request.urlopen(url).read().decode("utf-8")
data = json.loads(content)
result = data['weatherinfo']
Weather = json.loads(content)
cityZIDIAN = Weather["weatherinfo"]
cityInformation = ("%s\n%s\n%s ~ %s") % (cityZIDIAN['city'], cityZIDIAN['weather'],result['temp1'],result['temp2'])
return cityInformation
else:
win32api.MessageBox(0, "There are no this City\n\a没有該城市!", "今天天气")
root = tk.Tk()
app=Application(root)
#app.chaxun(app.create_widgets())
win32api.MessageBox(0,app.chaxun(app.create_widgets()),"")
app.mainloop()
年轻人,你这个代码看似很简单,就十几行代码。但是,里面涉及的知识点可不少,有gui,有http请求,有json等。
而看你贴出的第一张图的错误是参数不匹配。这是一个比较简单的错误,说明你对Python不熟悉。
建议你逊于渐进的学习,否则会事倍功半。先看基础,基础看完了,再看深入的。