如何从pythonturtle图形发送命令到EV3乐高积木?

2024-06-09 06:30:30 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑T KINTER:

IDE是visualstudio代码

回溯调用打印在脚本下面

TkinterTest.py

#!/usr/bin/env python3

from tkinter import *
from tkinter import ttk
import Ev3_Motor

ev3 = Ev3_Motor.Ev3_Motor()

def calculate(*args):
    ev3.TestFunction("SUCCESSS YAHOOOOOO")
    print("command to robot >>>>")

root = Tk()
root.title("TEST TKINTER")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

ttk.Button(mainframe, text="TEST BUTTON", command=calculate).grid(column=3, row=3, sticky=W)

#ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

root.bind('<Return>', calculate)
root.mainloop()

Ev3_电机.py

^{pr2}$

回溯错误:

Starting: brickrun --directory="/home/robot/vscode-hello-python-master/Ev3" "/home/robot/vscode-hello-python-master/Ev3/TkinterTest.py"
Started.
----------
Traceback (most recent call last):
  File "/usr/lib/python3.5/tkinter/__init__.py", line 36, in <module>
    import _tkinter
ImportError: No module named '_tkinter'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/robot/vscode-hello-python-master/Ev3/TkinterTest.py", line 3, in <module>
    from tkinter import *
  File "/usr/lib/python3.5/tkinter/__init__.py", line 38, in <module>
    raise ImportError(str(msg) + ', please install the python3-tk package')
ImportError: No module named '_tkinter', please install the python3-tk package
----------
Exited with error code 1.

**原始问题:我想做什么:**

我正在尝试创建一个程序设计,其中使用Python中的Turtle图形库创建的UI程序直接与乐高EV3积木上的EV3 Python程序通信。在

我目前所掌握的情况:

  1. 在前端.py-一种面向对象的程序,根据鼠标指针x和y以及用于绘制按钮状对象的形状来感知独特的按钮单击
  2. 在RobotInstruction.py-一个基于函数调用转动电机的EV3程序

当我试图让它工作时发生了什么:

似乎在依赖关系上有冲突。 像乌龟和ev3是不兼容的。在我看来前端.py文件试图加载到RobotInstruction.py锉成一团,最后在砖头上,这不是我想要的。在

重要提示:

独立地,这些脚本可以很好地工作。例如,RobotInstruction.py可接收键盘输入,作用于电机。我只想让“命令”从图形程序启动

我第一次尝试Janky超低效工作区:

——现有代码摘录附在末尾--

使用前端.py将字符串命令写入文件并 有RobotInstruction.py不断地读取该文件以获取命令,然后根据该命令,调用相关函数来转动电机。在

什么有效:

可以使用来自的命令成功写入文件前端.py在

无法从同一文件中成功读取命令

但是

它不是实时发生的。 我对Python的文件读写不是很熟悉…很有可能我做了一些尴尬的事情。。。在

我的问题:

我想做的可能吗? 你能点击一个乌龟图形创建按钮向一个ev3机器人发送命令吗?如果是这样,我将如何在两个单独的脚本之间建立连接?在

编写“摘录”

前端.py

def TurnTier(ButtonName):
   if ButtonName == "TurnT1":
      fileName = open("file1.txt", "w+")
      fileName.write("TurnT1")
      fileName.close()

RobotInstruction.py

while (not blnTierFound):
   # file1.txt is written to from FrontEnd.py through a button click
   # We are writing "TurnT1" into file1.txt
   # Here we are opening the same file for reading to check for changes

   fileName = open("file1.txt", "r+")
   ButtonName = fileName.read()
   fileName.close()

   ButtonName = str(ButtonName)
   if (ButtonName == "TurnT1"):
        blnTierFound = True
        strMotor = 'A'

   # In the main part of the code 
   motorLeft = fncStartMotor(strMotor)

Tags: 文件thefrompyimport命令程序tkinter
1条回答
网友
1楼 · 发布于 2024-06-09 06:30:30

重要信息

通常,EV3是用乐高的基于块的编程语言编程的。默认操作系统是用这种语言编程的。为了使用基于文本的编程语言(如Python)与机器人通信,必须安装一个名为ev3dev的新操作系统,该系统基于Linux,使用双引导SD卡。完整的安装说明here。在运行以下脚本之前,此设置是必需的。在

在评论部分的讨论之后,我提出了一个可能对您有用的解决方案。这使用了问题中的Tkinter脚本(已经过测试,可以正常工作),并且Ev3_Motor脚本已经修改为包括Ev3_Motor类(这使得导入脚本和创建该类的对象变得容易)。但是,此脚本未经测试,可能会产生其他错误,因为我没有Ev3 robot。这些错误可以在以后调试。确保Ev3_电机.py在同一目录中TkinterTest.py. 在

Ev3_电机.py

#!/usr/bin/env python3
from ev3dev.ev3 import *
import os
import sys
from time import sleep
import shutil 
import fileinput
import debug_print

os.system('setfont Lat15-TerminusBold14')

## Main Ev3 Motor Class ##
class Ev3_Motor:

    def __init__(self):
        debug_print("Constructor Ev3")

    def TestFunction(randomString):
        debug_print("Connection established: " + randomString)

TkinterTest.py

^{pr2}$

相关问题 更多 >