不熟悉Tkinter

时间:2015-06-05 20:12:22

标签: python tkinter raspberry-pi gpio

现在我有一个Raspberry pi(B +)连接到一个由gpio值操纵的电机控制器(L928n)。我是python的初学者。电机控制器控制rc卡车电机。 “W”向前走,“A”向左走,“D”向右走,“S”走向停止。脚本无法正常工作..我做错了什么?也可以让Tkinter确定何时按下按键而不是按下按键?不确定Tkinter是否应该用于此类项目。也许pygame会更好用吗?

#!/usr/bin/python2.7
import RPi.GPIO as gpio
import time
import sys
import Tkinter as tk 

def init():

   gpio.setmode(gpio.BOARD)
   gpio.setup(7, gpio.OUT)
   gpio.setup(11, gpio.OUT)
   gpio.setup(13, gpio.OUT)
   gpio.setup(15, gpio.OUT)
   gpio.setup(3, gpio.OUT)
   gpio.output(3, False)

def forward(tf):
    gpio.output(7, True)
    gpio.output(11, False)
    gpio.output(13, False)
    gpio.output(15, False)
    time.sleep(tf)
    gpio.cleanup

def stop(tf):
    gpio.output(7, False)
    gpio.output(11, False)
    gpio.output(13, False)
    gpio.output(15, False)
    time.sleep(tf) 
    gpio.cleanup()

def left(tf):
    gpio.output(7, True)
    gpio.output(11, False)
    gpio.output(13, False)
    gpio.output(15, False)
    time.sleep(tf)
    gpio.cleanup()

def right(tf):
    gpio.output(7, False)
    gpio.output(11, True)
    gpio.output(13, False)
    gpio.output(15, False)
    time.sleep(tf)
    gpio.cleanup()

def reverse(tf):
    gpio.output(7, False)
    gpio.output(11, True)
    gpio.output(13, False)
    gpio.output(15, False)
    time.sleep(tf)
    gpio.cleanup()          

def key_input(event):
    init()
    print 'Key:',event.char
    key_press = event.char

def key_press(event):
    init()
    key_press == event.char
    sleep_time = 0.030

if key_press == 'w':
    forward(sleep_time)
elif key_press == 's':
    stop(key_press)
elif key_press == 'a': 
    left()
elif key_press == 'd':
    right(sleep_time)
elif key_press == 'x':
    reverse(key_press)

command = tk.Tk()
command.bind('<KeyPress>', key_input)
command.mainloop()

2 个答案:

答案 0 :(得分:0)

以下声明块:

if key_press == 'w':
    forward(sleep_time)
elif key_press == 's':
    stop(key_press)
elif key_press == 'a': 
    left()
elif key_press == 'd':
    right(sleep_time)
elif key_press == 'x':
    reverse(key_press)

永远不会执行,因为它位于缩进的最外层,只读取一次(在函数定义之后)。您需要正确缩进并将其包含在相应的key_presskey_input函数中。

编辑:

def key_input(event):
    init()
    print 'Key:',event.char
    key_press = event.char
    checkKeyPress(key_press)

def key_press(event):
    init()
    key_press == event.char
    sleep_time = 0.030 
    checkKeyPress(key_press)

def checkKeyPress(key_press):
    if key_press == 'w':
        forward(sleep_time)
    elif key_press == 's':
        stop(key_press)
    elif key_press == 'a': 
        left()
    elif key_press == 'd':
        right(sleep_time)
    elif key_press == 'x':
        reverse(key_press)

我认为你的错误是你认为对key_press()的函数调用是某种goto - 它不是。调用该函数,在它结束后,您将跳转到您调用它的代码中的点。您还应该在python中对变量作用域进行一些阅读。

答案 1 :(得分:0)

我通过使用Tkinter以外的其他东西来解决问题...

#!/usr/bin/env python 
import RPi.GPIO as gpio
import time, sys

class _Getch:
    def __init__(self):
        try:
            self.impl = _GetchWindows()
        except ImportError:
            self.impl = _GetchUnix()

    def __call__(self): return self.impl()

class _GetchUnix:
    def __init__(self):
        import tty, sys

    def __call__(self):
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch

class _GetchWindows:
    def __init__(self):
        import msvcrt

    def __call__(self):
        import msvcrt
        return msvcrt.getch()

def Setup():
    gpio.setmode(gpio.BCM)

    #right
    gpio.setup(4, gpio.OUT)
    gpio.setup(17, gpio.OUT)
    gpio.setup(23, gpio.OUT)
    gpio.setup(24, gpio.OUT)
    gpio.setup(22, gpio.OUT)

    #right init are just "on"
    gpio.output(22, True)

    #left
    gpio.setup(12, gpio.OUT)
    gpio.setup(16, gpio.OUT)
    gpio.setup(18, gpio.OUT)


def QuitIt():
    sys.exit()

def getInput():
    got = int(raw_input('--> '))
    return got

# Right Forward gpio.output(15, True)
# Right Reverse gpio.output(13, True)

# Left  Forward gpio.output(18, True)
# Left  Reverse gpio.output(22, True)


def action(key):
    global delay
    if key == 'w':
        print "Forward"
        gpio.output(23, True)
        gpio.output(24, True)
        time.sleep(delay)
        gpio.output(23, False)
        gpio.output(24, False)

    if key == 's':
        print "Reverse"
        gpio.output(23, True)
        gpio.output(24, True)
        time.sleep(delay)
        gpio.output(23, False)
        gpio.output(24, False)

    if key == 'a':
        print "Left Pivot"
        gpio.output(4, False)
        gpio.output(17, True)
        time.sleep(delay)
        gpio.output(4, False)
        gpio.output(17, False)

    if key == 'd':
        print "Right Pivot"
        gpio.output(23, True)
        gpio.output(24, True)
        time.sleep(delay)
        gpio.output(23, False)
        gpio.output(24, False)

    if key == 'l':
        delay = delay + 0.1
        print ("Delay Up: " + str(delay))

    if key == 'k':
        delay = delay - 0.1
        print ("Delay Dw: " + str(delay))

    if key == 'q':
        print "Quit"
        QuitIt()

delay = 0.2

getch = _Getch()

Setup()

while True:
    action(getch())