I'ḿ trying to implement a python program on my RPI4 to control a stepper motor (via a Tic driver from Pololu) with a TKinter GUI. (I know that there are other libraries like PyTic or SMBus to control a Tic driver, but because of another Hall sensor I'm using, I have to use Pigpio).
Anyway, I thought it would be a simple problem, but Iḿ stuck on something weird: If I call the function "run(moteur)" inside my program, it goes well, If I call the function by clicking on the button of TKinter "command=lambda :run(Moteur1)", I have errors
My program :
import tkinter as tk
from tkinter import ttk, Button
import pigpio
class MonMoteur:
def __init__(self, bus, addresse):
# Initialisation de la connexion Pigpio
self.pi = pigpio.pi()
# Ouvrir la connexion I2C avec le bus et l'adresse spécifiés
self.h = self.pi.i2c_open(bus, addresse)
def set_target_position(self, target):
# On envoie la position voulue
command = [0xE0,
target >> 0 & 0xFF,
target >> 8 & 0xFF,
target >> 16 & 0xFF,
target >> 24 & 0xFF]
self.pi.i2c_write_device(self.h, command)
return "Position cible définie : {}".format(target)
def fermer_connexion(self):
# Fermeture de la connexion Pigpio
self.pi.i2c_close(self.h)
self.pi.stop()
addresseDuTic = 0x0E
bus = 3
Moteur1 = MonMoteur(bus, addresseDuTic)
commandeTic_Position = 0xE0
commandeTic_ExitSafeStart = 0x83
position_cible = 0xC8
# root window
root = tk.Tk()
root.title('Motor')
root.geometry('500x300')
value = 0
def run(moteur):
global value
value += 1
print(value)
moteur.set_target_position(200)
run(Moteur1)
# Label pour faie bouger le moteur
position_label = tk.Label(root, text="Faire bouger le moteur : ")
position_label.pack()
# button
button = ttk.Button(root, text='run', command=lambda :run(Moteur1))
button.pack(ipadx=5, ipady=5)
Moteur1.pi.stop()
root.mainloop()
And those are my errors if I'm clicking on the "Run" button of the TKinter GUI :
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
File "<string>", line 72, in <lambda>
File "<string>", line 63, in run
File "<string>", line 24, in set_target_position
File "/usr/local/lib/python3.9/dist-packages/pigpio.py", line 3186, in i2c_write_device
return _u2i(_pigpio_command_ext(
File "/usr/local/lib/python3.9/dist-packages/pigpio.py", line 1062, in \_pigpio_command_ext
sl.s.sendall(ext)
AttributeError: 'NoneType' object has no attribute 'sendall'
If somebody understands what's the problem it would be awesome :) Still testing in the meantime. Thanks a lot for the reading time in all cases. Bastien