我的目标是 运行 通过 Python 步进器,但是 angular 速度和度数等值是由用户通过 GUI 输入的

My goal is to run a Stepper through Python, but the values such as angular velocity and degrees are inputted by user through GUI

我遇到了 Tkinter window 不显示其内容以及将用户输入的数据存储到步进参数中的问题。我在不同的代码中找到了 运行 Tkinter 和步进器,它工作了。但我无法整合所有内容。这是我的代码:

import time
from tkinter import *
from pymata4 import pymata4

top = Tk()
top.geometry("450x300")
pins = [8,9,10,11]
board = pymata4.Pymata4()
board.set_pin_mode_stepper(num_steps, pins)
num_steps = Label(top, text="Steps").place(x=40, y=60)
angular_velocity = Label(top, text="Angular Velocity").place(x=40,y=100)
submit_button = Button(top, text ="Submit").place(x=40,y=130)
num_steps_input_area = Entry(top, width=30).place(x=180,y=60)
angular_velocity_input_area = Entry(top, width=30).place(x=180, y=100)
angularvelocity = angular_velocity_input_area.get()
numsteps = num_steps_input_area.get()

while True:
    board.stepper_write(angularvelocity, numsteps)
    time.sleep(1)
    board.stepper_write(angularvelocity, numsteps)
    time.sleep(1)

top.mainloop()

我不得不移动很多东西。由于我不熟悉原始代码的所有上下文,因此可能仍然存在一些问题。但是,希望它足够好,您可以看到如何实现您的目标。 我查看了 pymata4 模块,发现您的操作存在一些问题。

from tkinter import *
from pymata4 import pymata4

top = Tk()
top.geometry("450x300")
pins = [8,9,10,11]
board = pymata4.Pymata4()

# These variables need to be declared to be in the global space.
total_steps = 512 # Total number of steps per revolution of the stepper motor.
motorspeed = 0 # The library does not use angular velocity, but speed.
numsteps = 0  # To change direction use negative int, according to the module documentation.
complete = False  # I added this to show how to stop running.


def actuate():
    global motorspeed, numsteps, complete
    board.stepper_write(motorspeed, numsteps)
    if complete:
        pass
    else:
        top.after(1000, actuate)  # This will run actuate after 1000 ms
    

def set_input():
    global pins, motorspeed, numsteps
    motorspeed = motor_speed_var.get() # run .get() on IntVar object instead
    numsteps = num_steps_var.get()  # run .get() on IntVar object instead
    board.set_pin_mode_stepper(total_steps, pins)  # it was num_steps?
    actuate()

num_steps = Label(top, text="Steps").place(x=40, y=60)
motor_speed = Label(top, text="Angular Velocity").place(x=40,y=100)
# Button requires a command definition or it is just for looks
submit_button = Button(top, text ="Submit" command=set_input).place(x=40,y=130)
num_steps_var = IntVar()  # These are tkinter built in. There is also a StringVar
num_steps_input_area = Entry(top, width=30, textvariable=num_steps_var).place(x=180,y=60)
motor_speed_var = IntVar() # and a BooleanVar
motor_speed_input_area = Entry(top, width=30, textvariable=motor_speed_var).place(x=180, y=100)



top.mainloop()

让我知道它是否适合您。我没有 运行 这个。