ajax无线遥控器,利用python+tkinter做一个简单的智能电视遥控器

要通过python实现遥控器功能分两步:

第一步:开发图形化界面,以暴风TV的遥控器按钮为例

第二步:使PC端给电视发送相应指令(此步骤需要打开电视的adb开关)

现在就开始第一步操作实现遥控器功能,python2输入以下代码

注意:python3需要将代码中的from Tkinter import *

替换为from tkinter import *

将from SimpleDialog import *

替换为import tkinter.simpledialog

#coding=utf-8from Tkinter import *

from SimpleDialog import *

'''

from tkinter import *

import tkinter.simpledialog

'''

root=Tk()

#设置图形的宽高

root.geometry('120x200')

#设置图形为不可拉升

root.resizable(0, 0)

#设置图形的标题

root.title('遥控器')

#添加组件布局

Button(root,text="电源",command="").place(x=45,y=0,)

Button(root,text="左键",command="").place(x=0,y=60,height=30)

Button(root,text="右键",command="").place(x=90,y=60,height=30)

Button(root,text="上键",command="").place(x=45,y=30,height=30)

Button(root,text="下键",command="").place(x=45,y=90,height=30)

Button(root,text="OK",command="").place(x=40,y=60,height=30,width=45)

Button(root,text="主页",command="").place(x=0,y=130,height=30,width=30)

Button(root,text="返回",command="").place(x=45,y=130,height=30,width=30)

Button(root,text="菜单",command="").place(x=90,y=130,height=30,width=30)

Button(root,text="音+",command="").place(x=0,y=170,height=30,width=30)

Button(root,text="音-",command="").place(x=45,y=170,height=30,width=30)

Button(root,text="BIU",command="").place(x=90,y=170,height=30,width=30)

mainloop()

运行成功后,Python2界面显示如左图,Python3界面显示如右图

6ff283ced3982419fcf7f365305e5565.png

a3fe602d82fb889c71a8554f9b81c782.png

第二步,在command中加入相应的键值对应的方法,可以先定义好各个方法,举个例子,比如电视的确定键对应的键值是23,那么我们先定义一个ok键对应的方法

defok():

subprocess.call('adb shell input keyevent 23', shell=True)

同时,考虑到按ok键还没执行完毕,我们又按了其它键,这样就需要加入多线程,方法如下

defok_1():

subprocess.call('adb shell input keyevent 23', shell=True)defok():

t=threading.Thread(target=ok_1)

t.start()

这样,简单的遥控器功能,我们就做完了:

#coding=utf-8

from Tkinter import *

from SimpleDialog import *

importsubprocessimportthreading'''from tkinter import *

import tkinter.simpledialog'''

defup_1():

subprocess.call('adb shell input keyevent 19', shell=True)defup():

t=threading.Thread(target=up_1)

t.start()defdown_1():

subprocess.call('adb shell input keyevent 20', shell=True)defdown():

t=threading.Thread(target=down_1)

t.start()defleft_1():

subprocess.call('adb shell input keyevent 21', shell=True)defleft():

t=threading.Thread(target=left_1)

t.start()defright_1():

subprocess.call('adb shell input keyevent 22', shell=True)defright():

t=threading.Thread(target=right_1)

t.start()defhome_1():

subprocess.call('adb shell input keyevent 3', shell=True)defhome():

t=threading.Thread(target=home_1)

t.start()defback_1():

subprocess.call('adb shell input keyevent 4', shell=True)defback():

t=threading.Thread(target=back_1)

t.start()defok_1():

subprocess.call('adb shell input keyevent 23', shell=True)defok():

t=threading.Thread(target=ok_1)

t.start()defvoice_1():

subprocess.call('adb shell input keyevent 24', shell=True)defvoice1():

t=threading.Thread(target=voice_1)

t.start()defvoice_2():

subprocess.call('adb shell input keyevent 25', shell=True)defvoice2():

t=threading.Thread(target=voice_2)

t.start()defmenu_1():

subprocess.call('adb shell input keyevent 82', shell=True)defmenu():

t=threading.Thread(target=menu_1)

t.start()defpower_1():

subprocess.call('adb shell input keyevent 754', shell=True)defpower():

t=threading.Thread(target=power)

t.start()defbiu_1():

subprocess.call('adb shell input keyevent 733', shell=True)defbiu():

t=threading.Thread(target=biu_1)

t.start()

root=Tk()#设置图形的宽高

root.geometry('120x200')#设置图形为不可拉升

root.resizable(0, 0)#设置图形的标题

root.title('遥控器')#添加组件布局

Button(root,text="电源",command=power).place(x=45,y=0,)

Button(root,text="左键",command=left).place(x=0,y=60,height=30)

Button(root,text="右键",command=right).place(x=90,y=60,height=30)

Button(root,text="上键",command=up).place(x=45,y=30,height=30)

Button(root,text="下键",command=down).place(x=45,y=90,height=30)

Button(root,text="OK",command=ok).place(x=40,y=60,height=30,width=45)

Button(root,text="主页",command=home).place(x=0,y=130,height=30,width=30)

Button(root,text="返回",command=back).place(x=45,y=130,height=30,width=30)

Button(root,text="菜单",command=menu).place(x=90,y=130,height=30,width=30)

Button(root,text="音+",command=voice1).place(x=0,y=170,height=30,width=30)

Button(root,text="音-",command=voice2).place(x=45,y=170,height=30,width=30)

Button(root,text="BIU",command=biu).place(x=90,y=170,height=30,width=30)

mainloop()

mainloop()

如何使其能操控电视,需要我们先使用adb命令连接电视ip,在cmd下,输入格式为:adb connect 192.168.XX.XXX

3a93644dcb8bb66cacb491ac8a9328dc.png

若连接成功后,输入adb devices显示 192.168.XX.XXX devices,表示连接成功,之后就可以用电脑上的遥控器控制电视了

2149772b3331b66f7735811fd895ca20.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值