Rc汽车GUI tkinter

时间:2017-11-03 11:59:39

标签: python tkinter

我一般都是python和编码的新手,所以我有点卡在我需要一些指导的地方。

我目前正在从学校开展一个项目,在那里我正在建立一个带有树莓派3的遥控车。我正在为Tkinter的汽车用户界面工作。代码如下,我要求的是如何通过点击GUI上的按钮,以及如何在其上添加推送和释放功能,使汽车向前,向后,向左和向右前进。

如您所见,我通过远程GPIO(Gpiozero)运行它。另外,我只是在这里添加左侧按钮以作为其余代码的示例。

我在按钮上有一张照片图片,但是如果你想运行代码,只需要#tit,然后在tk.button中输入文字

    import tkinter as tk
    import sys
    from PIL import Image, ImageTk
    import time
    from gpiozero import motor
    from gpiozero.pins.pigpio import PiGPIOFactory

    #remote_factory = PiGPIOFactory(host=)
    turn_speed = 0.5
    drive_speed = 0.15
    #remote_factory = PiGPIOFactory(host=)
    # 19 = FORWARD, 13 = BACKWARD
    motor_1 = Motor(forward=19, backward=13)
    # 6 = LEFT, 5 = RIGHT
    motor_2 = Motor(forward=6, backward=5)

    drive_forward = motor_1.forward
    drive_backward = motor_1.backward
    turn_left = motor_2.forward
    turn_right = motor_2.backward

    #motor_forward = Motor(forward=19)
    #motor_reverse = Motor(backward=13)
    #motor_left = Motor(forward=6)
    #motor_right = Motor(backward=5)





    def main():
        vindu=tk.Tk()
        vindu.title("PiDrv")
        vindu.geometry("300x300")
       #vindu.configure


        #Turn left
        def left_arrow():

            print("Left")
            #turn_left

        def stop():
            print("Stop")

        def left_button():
            b = tk.Button(vindu, command=left_arrow )
            b.grid(row=1, column=1, padx=0, pady=150) 
            image=ImageTk.PhotoImage(file="Arrow_left2.jpg")
            b.config(image=image)
            b.image = image
            b.config(height=70, width=70)
            #b.bind("<Button-1>", left_arrow)
            #b.bind("<ButtonRelease-1>",stop)
            b.grid()
        left_button()

    main()

提前致谢:)

1 个答案:

答案 0 :(得分:0)

体面的问题imho,它带来了一个重要的问题; 如何在tkinter的mainloop 之外循环。答案是使用Tk的after(delay, callback),看看我起草的例子,随意运行它;

from tkinter import *
import sys
from PIL import Image, ImageTk
import time
from gpiozero import motor
from gpiozero.pins.pigpio import PiGPIOFactory

#remote_factory = PiGPIOFactory(host=)
turn_speed = 0.5
drive_speed = 0.15
#remote_factory = PiGPIOFactory(host=)
# 19 = FORWARD, 13 = BACKWARD
motor_1 = Motor(forward=19, backward=13)
# 6 = LEFT, 5 = RIGHT
motor_2 = Motor(forward=6, backward=5)

drive_forward = motor_1.forward
drive_backward = motor_1.backward
turn_left = motor_2.forward
turn_right = motor_2.backward

#motor_forward = Motor(forward=19)
#motor_reverse = Motor(backward=13)
#motor_left = Motor(forward=6)
#motor_right = Motor(backward=5)

class RcApp(Tk):

    def __init__(self):
        Tk.__init__(self)

        self.left = Button(self, text='go left!')
        self.right = Button(self, text='go right!')
        self.forward = Button(self, text='go forward!')
        self.reverse = Button(self, text='reverse!')

        self.bind('<ButtonPress>', self.callback)
        self.bind('<ButtonRelease>', self.callback)

        self.left.grid(column=0,row=1)
        self.right.grid(column=2,row=1)
        self.forward.grid(column=1,row=0)
        self.reverse.grid(column=1,row=2)

        self.type = None

    def callback(self, event):
        print(event.type)
        self.type = event.type
        if self.type == '4':
            if event.widget == self.left:
                self.after(200, self.move_left)
            if event.widget == self.right:
                self.after(200, self.move_right)
            if event.widget == self.forward:
                self.after(200, self.move_forward)
            if event.widget == self.reverse:
                self.after(200, self.move_reverse)

    def move_left(self):
        if self.type == '4':
            print('moving left!')
            turn_left()
            self.after(200, self.move_left)

    def move_right(self):
        if self.type == '4':
            print('moving right!')
            turn_right()
            self.after(200, self.move_right)

    def move_forward(self):
        if self.type == '4':
            print('moving forward!')
            drive_forward()
            self.after(200, self.move_forward)

    def move_reverse(self):
        if self.type == '4':
            print('moving reverse!')
            drive_backward()
            self.after(200, self.move_reverse)

root = RcApp()
root.mainloop()

有关after的更多信息,请访问this other question

祝你好运!