【python】利用python+tkinter做一個簡單的智能電視遙控器


要通過python實現遙控器功能分兩步:

第一步:開發圖形化界面,以暴風TV的遙控器按鈕為例

第二步:使PC端給電視發送相應指令(此步驟需要打開電視的adb開關)

現在就開始第一步操作實現遙控器功能,python2輸入以下代碼

注意:python3需要將代碼中的from Tkinter import *

           替換為from tkinter import *

           將from SimpleDialog import *

           替換為import tkinter.simpledialog

#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界面顯示如右圖

第二步,在command中加入相應的鍵值對應的方法,可以先定義好各個方法,舉個例子,比如電視的確定鍵對應的鍵值是23,那么我們先定義一個ok鍵對應的方法

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,表示連接成功,之后就可以用電腦上的遙控器控制電視了

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM