Python Tkinter小试

前两天看到一篇关于Python使用Tkinter 的博文,写的很好。就拿来研究了一下,改了改。现分享如下:

参考

代码

# coding:utf8
# python2.73 winxp
'''''
天气插件: 使用json接受中气象的本地信息,显示使用tkinter
url http://www.weather.com.cn/data/sk/101221201.html

v0.1
'''
from Tkinter import *
import json
import urllib
import time


def getWeaInfo():
    url = r'http://www.weather.com.cn/data/cityinfo/101221201.html'
    res = urllib.urlopen(url)  # 返回的是个json样式的字符串
    weatherinfo = {}
    jinfo = res.read().decode('utf8')  # jinfo是一个unicode对象,而我们要的是一个json,然后解析
    info = json.loads(jinfo)  # 变成键值对

    if info:
        try:
            weatherinfo['city'] = info['weatherinfo']['city']  # 城市
            weatherinfo['tempH'] = info['weatherinfo']['temp1']  # 高温
            weatherinfo['tempL'] = info['weatherinfo']['temp2']  # 低温
            weatherinfo['weather'] = info['weatherinfo']['weather']  # 天气
            weatherinfo['ptime'] = info['weatherinfo']['ptime']  # 发布时间
        except KeyError, e:
            print 'Do not get the key', e.message
    return weatherinfo


class WeatherFrame:
    '''''
    用于显示主框架
    '''

    def __init__(self):
        self.root = Tk()

        # 一个标题
        self.title = Label(self.root, text=u'天气情况')
        self.title.pack(side=TOP)

        # 四对信息框
        self.fm1 = Frame(self.root)
        self.label_city = Label(self.fm1, text=u'城市')
        self.label_city.pack(side=LEFT, expand=YES)
        self.text_city_e = StringVar()
        self.text_city = Entry(self.fm1, text='', state='readonly', textvariable=self.text_city_e)
        self.text_city.pack(side=LEFT, expand=YES)
        self.fm1.pack(side=TOP)

        self.fm2 = Frame(self.root)
        self.label_temph = Label(self.fm2, text=u'高温')
        self.label_temph.pack(side=LEFT, expand=YES)
        self.text_temph_e = StringVar()
        self.text_temph = Entry(self.fm2, text='', state='readonly', textvariable=self.text_temph_e)
        self.text_temph.pack(side=LEFT, expand=YES)
        self.fm2.pack(side=TOP)

        self.fm3 = Frame(self.root)
        self.label_templ = Label(self.fm3, text=u'低温')
        self.label_templ.pack(side=LEFT, expand=YES)
        self.text_templ_e = StringVar()
        self.text_templ = Entry(self.fm3, text='', state='readonly', textvariable=self.text_templ_e)
        self.text_templ.pack(side=LEFT, expand=YES)
        self.fm3.pack(side=TOP)

        self.fm4 = Frame(self.root)
        self.label_weather = Label(self.fm4, text=u'天气')
        self.label_weather.pack(side=LEFT, expand=YES)
        self.text_weather_e = StringVar()
        self.text_weather = Entry(self.fm4, text='', state='readonly', textvariable=self.text_weather_e)
        self.text_weather.pack(side=LEFT, expand=YES)
        self.fm4.pack(side=TOP)

        # 两个操作按钮
        self.fm5 = Frame(self.root)
        self.button_pull = Button(self.fm5, text=u'获取', command=self.pullWeaInfo)
        self.button_pull.pack(side=LEFT, expand=YES)
        self.button_quit = Button(self.fm5, text=u'退出', command=self.root.quit)
        self.button_quit.pack(side=LEFT, expand=YES)
        self.fm5.pack(side=TOP)

        self.pullWeaInfo()

    def pullWeaInfo(self):
        # 推送天气信息,初始化推送,手动更新
        weainfo = getWeaInfo()
        self.text_city_e.set(weainfo['city'])
        self.text_temph_e.set(weainfo['tempH'])
        self.text_templ_e.set(weainfo['tempL'])
        self.text_weather_e.set(weainfo['weather'])
        print ('lastest updated time is  %s' % time.ctime())


if __name__ == '__main__':
    wf = WeatherFrame()
    mainloop()

效果

别人的代码截图

我的修改

中国城市代码

城市代码获取

代码

# coding:utf-8
import sys

reload(sys)
sys.setdefaultencoding('utf8')
#    __author__ = '郭 璞'
#    __date__ = '2016/8/26'
#    __Desc__ = 天气预报小案例

from Tkinter import *
import urllib2
import json


def getWeatherInfoByCityCode(citycode):
    url = r'http://www.weather.com.cn/data/cityinfo/'+str(citycode)+'.html'
    data = urllib2.urlopen(url).read().decode('utf8')

    json_info = json.loads(data)
    weatherinfo = {}
    weatherinfo['city'] = json_info['weatherinfo']['city']  # 城市
    weatherinfo['tempH'] = json_info['weatherinfo']['temp2']  # 高温
    weatherinfo['tempL'] = json_info['weatherinfo']['temp1']  # 低温
    weatherinfo['weather'] = json_info['weatherinfo']['weather']  # 天气
    weatherinfo['ptime'] = json_info['weatherinfo']['ptime']  # 发布时间
    return weatherinfo

def show_in_tkinter(ccode):
    tk = Tk()
    citycode = StringVar()
    citycode.set(ccode)
    weatherinfo = getWeatherInfoByCityCode(citycode.get())

    frame1 = Frame(tk)
    Label(frame1,text='输入城市代码').pack(side=LEFT,expand=YES)
    Entry(frame1,textvariable=citycode).pack()
    frame1.pack(side=TOP)

    frame2 = Frame(tk)
    Label(frame2, text='城市名称').pack(side=LEFT, expand=YES)
    cityname = '%s'%weatherinfo['city']
    str_v_cityname = StringVar()
    str_v_cityname.set(cityname)
    Entry(frame2, textvariable=str_v_cityname,state='readonly').pack()
    frame2.pack(side=TOP)

    frame3 = Frame(tk)
    Label(frame3, text='     高温').pack(side=LEFT, expand=YES)
    temp_h = '%s' % weatherinfo['tempH']
    str_v_temp_h = StringVar()
    str_v_temp_h.set(temp_h)
    Entry(frame3, textvariable=str_v_temp_h, state='readonly').pack()
    frame3.pack(side=TOP)

    frame4 = Frame(tk)
    Label(frame4, text='     低温').pack(side=LEFT, expand=YES)
    temp_l = '%s' % weatherinfo['tempL']
    str_v_temp_l = StringVar()
    str_v_temp_l.set(temp_l)
    Entry(frame4, textvariable=str_v_temp_l, state='readonly').pack()
    frame4.pack(side=TOP)

    frame5 = Frame(tk)
    Label(frame5, text='     天气').pack(side=LEFT, expand=YES)
    weather = '%s' % weatherinfo['weather']
    str_v_weather = StringVar()
    str_v_weather.set(weather)
    Entry(frame5, textvariable=str_v_weather, state='readonly').pack()
    frame5.pack(side=TOP)

    frame6 = Frame(tk)
    Label(frame6, text='更新时间').pack(side=LEFT, expand=YES)
    updatetime = '%s' % weatherinfo['ptime']
    str_v_updatetime = StringVar()
    str_v_updatetime.set(updatetime)
    Entry(frame6, textvariable=str_v_updatetime, state='readonly').pack()
    frame6.pack(side=TOP)

    frame7 = Frame(tk)
    btn_pull = Button(frame7,text='获取数据',command=lambda:show_in_tkinter(citycode.get()))
    print citycode.get()
    # btn_pull.bind(citycode.get(),update_weather)
    btn_pull.pack(side=LEFT, expand=YES)
    btn_quit = Button(frame7,text='退出',command=tk.quit).pack(side=LEFT, expand=YES)
    frame7.pack(side=TOP)

    tk.mainloop()

if __name__=="__main__":
    show_in_tkinter(101180308)

测试结果

我的截图

不足之处

本来设计的是可以输入城市代码,来实时的获取对应的天气信息,并更新到界面上的,但是不知道为什么系统会默认新开一个Tkinter的界面,导致刷新失败!

有时间再来更新本文,打个时间戳:2016年8月26日22:50:14

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python tkinterPython 的 GUI 开发工具包,它为开发者提供丰富的 GUI 组件和工具,使得开发界面化的应用程序变得更加方便。在 Python tkinter 中可以创建按钮、文本框、列表框、滚动条等众多的组件,并且以面向对象的方式进行设计。使用 Python tkinter 进行答题程序的开发是十分便捷的。 在开发一个 Python tkinter 答题程序时,首先需要设计答题界面,包括题目、选项、倒计时等内容。通过 Python tkinter 中提供的 Label、Button、Radiobutton、Checkbutton、Scale 等组件进行设计,可以使得程序在界面上更加美观、易于操作。然后,在程序的逻辑部分中,需要设计答题的规则、分数积分、难度等级等内容。在这一过程中,要采用 Python 的知识结合 Python tkinter 的特性,确保程序的正确性和高效性。 此外,在 Python tkinter 答题程序中,可以使用 Python 的数据结构和多线程等相关技术进行优化,从而提高程序的性能和稳定性。例如,可以使用列表、字典等数据结构来存储题目和答案,使用多线程来提高程序的响应速度等。以此,设计一款高质量的 Python tkinter 答题程序是非常有益和有意义的。 总之, Python tkinter 答题程序是一种非常好的练手项目,它不仅可以锻炼 Python 编程能力,还可以让开发者更好地理解 GUI 应用程序的实现方式。无论是对于初学者还是有经验的开发者而言,Python tkinter 答题程序都是一个值得挑战的有趣项目。通过不断地改进和优化,开发者可以创造出更加稳定、高效、功能丰富的 Python tkinter 答题程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值