基于树莓派的python界⾯开发实例教程基于树莓派的python界⾯开发实例教程
环境测试
点击树莓派的开始菜单,到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')
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)
def create_widgets(self):
self.lable1 = Label(self, text='Welcome to my window!')
id(row=0, column=0, sticky= W)
root =Tk()
root.title('Test Application window with label1')
app=Application(root)python入门教程app
app.mainloop()
简述mvc
运⾏后结果如下:
时钟程序
from tkinter import*
import time
import threading
class Application(Frame):
"""Build the basic window frame template"""
def__init__(self, master):
grep命令原理super(Application, self).__init__(master)
def create_widgets(self):
t = time.localtime()
current_time = time.strftime('%H:%M:%S',t)
self.lable1 = Label(self, text=current_time)
id(row=0, column=0, sticky= NSEW)                fig(font=('Arial',370))
fig(bg='black',fg='white')
self.lable1.after(1000,self.Refresher)
def Refresher(self):
t = time.localtime()
current_time = time.strftime('%H:%M:%S',t)
figure(text=current_time)
self.lable1.after(1000,self.Refresher)
root =Tk()潮牌stacking
root.title('Test Application window with label1')
width= root.winfo_screenwidth()
height= root.winfo_screenheight()
app=Application(root)
app.mainloop()
运⾏后如下:
添加天⽓
时钟写完以后,觉得有点空洞,决定加⼊天⽓情况的信息。经初步调研,选择⽤‘openweathermap’的API,官⽹。
length怎么读英语发音此提供商有集成好的python包,注册⼀个账号可免费获取(⽬前免费版三⼩时更新⼀次)。需要导⼊“pyowm”包。
官⽹有。
代码如下(特别备注:pyowm.OWM括号⾥⾯的是API Key,,会⾃动⽣成⼀个,代码⾥的是⽰例,我已更改过,不可⽤,请替换成⾃⼰的):