要經過python實現遙控器功能分兩步:python
第一步:開發圖形化界面shell
第二步:使PC端給電視發送相應指令多線程
如今就開始第一步操做實現遙控器功能,python2輸入如下代碼oop
注意:python3須要將代碼中的from Tkinter import *佈局
替換爲from tkinter import *spa
將from SimpleDialog import *線程
替換爲import tkinter.simpledialogcode
#coding=utf-8
from 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界面顯示如右圖blog
第二步,在command中加入相應的鍵值對應的方法,能夠先定義好各個方法,舉個例子,好比電視的肯定鍵對應的鍵值是23,那麼咱們先定義一個ok鍵對應的方法ip
def ok(): subprocess.call('adb shell input keyevent 23', shell=True)
同時,考慮到按ok鍵還沒執行完畢,咱們又按了其它鍵,這樣就須要加入多線程,方法以下
def ok_1(): subprocess.call('adb shell input keyevent 23', shell=True) def ok(): t=threading.Thread(target=ok_1) t.start()
這樣,簡單的遙控器功能,咱們就作完了:
#coding=utf-8 from Tkinter import * from SimpleDialog import * import subprocess import threading ''' from tkinter import * import tkinter.simpledialog ''' def up_1(): subprocess.call('adb shell input keyevent 19', shell=True) def up(): t=threading.Thread(target=up_1) t.start() def down_1(): subprocess.call('adb shell input keyevent 20', shell=True) def down(): t=threading.Thread(target=down_1) t.start() def left_1(): subprocess.call('adb shell input keyevent 21', shell=True) def left(): t=threading.Thread(target=left_1) t.start() def right_1(): subprocess.call('adb shell input keyevent 22', shell=True) def right(): t=threading.Thread(target=right_1) t.start() def home_1(): subprocess.call('adb shell input keyevent 3', shell=True) def home(): t=threading.Thread(target=home_1) t.start() def back_1(): subprocess.call('adb shell input keyevent 4', shell=True) def back(): t=threading.Thread(target=back_1) t.start()
def ok_1(): subprocess.call('adb shell input keyevent 23', shell=True) def ok(): t=threading.Thread(target=ok_1) t.start() def voice_1(): subprocess.call('adb shell input keyevent 24', shell=True)
def voice1(): t=threading.Thread(target=voice_1) t.start() def voice_2(): subprocess.call('adb shell input keyevent 25', shell=True)
def voice2(): t=threading.Thread(target=voice_2) t.start() def menu_1(): subprocess.call('adb shell input keyevent 82', shell=True)
def menu(): t=threading.Thread(target=menu_1) t.start() def power_1(): subprocess.call('adb shell input keyevent 754', shell=True) def power(): t=threading.Thread(target=power) t.start() def biu_1(): subprocess.call('adb shell input keyevent 733', shell=True) def biu(): 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
若鏈接成功後,輸入adb devices顯示 192.168.XX.XXX devices,表示鏈接成功,以後就能夠用電腦上的遙控器控制電視了