基于树莓派的python界面开发实例教程
- 环境测试
- 添加label实例
- 时钟程序
- 添加天气
环境测试
点击树莓派的开始菜单,找到programming-Python3(IDLE),点击打开。
打开后如下:
在home/pi下面建立home/pi/py3prog的文件夹,专门用来存放python脚本,利用IDLE来新建一个home/pi/py3prog/digitalClock.py文件,用来存放让实例文件。
用IDLE打开刚刚新建的文件,填入以下带代码:
from tkinter import *
root = Tk()
root.titlle('this is my digital clock')
root.geometry('300x100')
app = Application(root)
app.mainloop()
保存,运行,应当出现以下界面:
添加label实例
特别注意的是__init__的下划线是由两段‘_’组成的,如果拼写为一个下划线,虽然不报错,但是运行后控件无效,不显示。
from tkinter import *
class Application(Frame):
"""Build the basic window frame template"""
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.lable1 = Label(self, text='Welcome to my window!')
self.lable1.grid(row=0, column=0, sticky= W)
root =Tk()
root.title('Test Application window with label1')
root.geometry('300x100')
app=Application(root)
app.mainloop()
运行后结果如下:
时钟程序
from tkinter import *
import time
import threading
class Application(Frame):
"""Build the basic window frame template"""
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
t = time.localtime()
current_time = time.strftime('%H:%M:%S',t)
self.lable1 = Label(self, text=current_time)
self.lable1.grid(row=0, column=0, sticky= NSEW)
self.lable1.config(font=('Arial',370))
self.lable1.config(bg='black',fg='white')
self.lable1.after(1000,self.Refresher)
def Refresher(self):
t = time.localtime()
current_time = time.strftime('%H:%M:%S',t)
self.lable1.configure(text=current_time)
self.lable1.after(1000,self.Refresher)
root =Tk()
root.title('Test Application window with label1')
width= root.winfo_screenwidth()
height= root.winfo_screenheight()
root.geometry("%dx%d" % (width, height))
root.config(bg='black')
app=Application(root)
app.mainloop()
运行后如下:
添加天气
时钟写完以后,觉得有点空洞,决定加入天气情况的信息。经初步调研,选择用‘openweathermap’的API,官网见此。
此提供商有集成好的python包,注册一个账号可免费获取(目前免费版三小时更新一次)。需要导入“pyowm”包。
官网有示例文档。
代码如下(特别备注:pyowm.OWM括号里面的是API Key,去官网注册一个账户,会自动生成一个,代码里的是示例,我已更改过,不可用,请替换成自己的):
from tkinter import *
import time
import threading
import pyowm
import json
class Application(Frame):
"""Build the basic window frame template"""
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
t = time.localtime()
current_time = time.strftime('%H:%M:%S',t)
self.lable1 = Label(self, text=current_time)
self.lable1.grid(row=0, column=0, sticky= NSEW)
self.lable1.config(font=('Arial',370))
self.lable1.config(bg='black',fg='white')
self.lable1.after(1000,self.Refresher)
owm = pyowm.OWM('02f7e0006144dc293dcd49ec8c4750dd')
mgr = owm.weather_manager()
weather = mgr.weather_at_place('Shanghai,CN').weather
# sf = owm.weather_at_place('London,uk')
temp_dict_celsius = weather.temperature('celsius')
wetherInfo = json.dumps(temp_dict_celsius)
self.labelweatherlocal = Label(self,text = current_time + wetherInfo)
self.labelweatherlocal.grid(row=1, column=0, sticky= NSEW)
self.labelweatherlocal.config(font=('Arial',34))
self.labelweatherlocal.config(bg='black',fg='white')
self.labelweatherlocal.after(600000,self.weatherRefresher)
def Refresher(self):
t = time.localtime()
current_time = time.strftime('%H:%M:%S',t)
self.lable1.configure(text=current_time)
self.lable1.after(1000,self.Refresher)
def weatherRefresher(self):
t = time.localtime()
current_time = time.strftime('%H:%M:%S',t)
owm = pyowm.OWM('02f7e0006144dc293dcd49ec8c4750dd')
mgr = owm.weather_manager()
weather = mgr.weather_at_place('Shanghai,CN').weather
# sf = owm.weather_at_place('London,uk')
temp_dict_celsius = weather.temperature('celsius')
wetherInfo = json.dumps(temp_dict_celsius)
self.labelweatherlocal.configure(text = current_time + wetherInfo)
self.labelweatherlocal.after(600000,self.weatherRefresher)
root =Tk()
root.title('Test Application window with label1')
width= root.winfo_screenwidth()
height= root.winfo_screenheight()
root.geometry("%dx%d" % (width, height))
root.config(bg='black')
app=Application(root)
app.mainloop()
运行结果如下:
下面返回的是时间(用以显示更新时间,目前十分钟更新一次),后面会写一个好看的窗口把数据搞得好看些。目前返回信息依次是当前温度、最高温度、最低温度、体感温度。作者在上海,所以参数传进去的是上海。