码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-06-10 18:48:28      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:start   sim   man   宽高   comm   python3   target   connect   oop   

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

第一步:开发图形化界面

第二步:使PC端给电视发送相应指令

现在就开始第一步操作实现遥控器功能,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,表示连接成功,之后就可以用电脑上的遥控器控制电视了

技术图片

 

 

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

标签:start   sim   man   宽高   comm   python3   target   connect   oop   

原文地址:https://www.cnblogs.com/ffrs/p/10999357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!